<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>C语言笔记 | Mox的笔记库</title><meta name="keywords" content="Working"><meta name="author" content="MocusEZ"><meta name="copyright" content="MocusEZ"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="今年暑假的学习笔记，4k-5k字(算上示例代码)左右，配合目录食用更佳">
<meta property="og:type" content="article">
<meta property="og:title" content="C语言笔记">
<meta property="og:url" content="https://www.mocusez.site/posts/414f.html">
<meta property="og:site_name" content="Mox的笔记库">
<meta property="og:description" content="今年暑假的学习笔记，4k-5k字(算上示例代码)左右，配合目录食用更佳">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=238395782,2024873914&fm=26&gp=0.jpg">
<meta property="article:published_time" content="2020-08-19T02:30:26.000Z">
<meta property="article:modified_time" content="2020-12-06T07:26:26.000Z">
<meta property="article:author" content="MocusEZ">
<meta property="article:tag" content="Working">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=238395782,2024873914&fm=26&gp=0.jpg"><link rel="shortcut icon" href="/img/title.jpg"><link rel="canonical" href="https://www.mocusez.site/posts/414f"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?c85c9eaebc158345532b86397a6dded9";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'C语言笔记',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2020-12-06 15:26:26'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.2.0"><link rel="alternate" href="/atom.xml" title="Mox的笔记库" type="application/atom+xml">
</head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/head.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">61</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">0</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链&amp;私人收藏</span></a></div><div class="menus_item"><a class="site-page" href="/board/"><i class="fa-fw fas fa-user"></i><span> 留言板</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=238395782,2024873914&amp;fm=26&amp;gp=0.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">Mox的笔记库</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链&amp;私人收藏</span></a></div><div class="menus_item"><a class="site-page" href="/board/"><i class="fa-fw fas fa-user"></i><span> 留言板</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">C语言笔记</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-08-19T02:30:26.000Z" title="发表于 2020-08-19 10:30:26">2020-08-19</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2020-12-06T07:26:26.000Z" title="更新于 2020-12-06 15:26:26">2020-12-06</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%97%A5%E5%B8%B8%E7%AC%94%E8%AE%B0/">日常笔记</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="C语言笔记"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>2020年11月12日大更新：根据课堂内容，一些微信公众号，ACM竞赛进行补充，字数(算上示例代码)已达11k</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">printf</span>(<span class="string">&quot;hello Wolrd&quot;</span>)</span><br></pre></td></tr></table></figure>

<p>资料来源：<br><a target="_blank" rel="noopener external nofollow noreferrer" href="https://www.runoob.com/cprogramming/c-tutorial.html">runoob.com</a><br>谭浩强的《C程序设计》<br><strong>如有出错还请大家评论指正</strong></p>
<p>段<em>错误</em>(<em>segment</em> <em>error</em>)</p>
<p>allocated  拨…(给);                    划…(归);                  分配…(给);</p>
<p>modifiable 可修改的</p>
<p>static 修饰符也可以应用于全局变量。<strong>当 static 修饰全局变量时，会使变量的作用域限制在声明它的文件内</strong>。</p>
<h2 id="大学考前突击"><a href="#大学考前突击" class="headerlink" title="大学考前突击"></a>大学考前突击</h2><p>既然是考试，该突击就要突击，大部分平时都用不上</p>
<p><a target="_blank" rel="noopener external nofollow noreferrer" href="https://imgchr.com/i/D3bUtU"><img src="https://s3.ax1x.com/2020/11/22/D3bUtU.md.jpg" alt="D3bUtU.md.jpg"></a></p>
<p><a target="_blank" rel="noopener external nofollow noreferrer" href="https://imgchr.com/i/D3bahF"><img src="https://s3.ax1x.com/2020/11/22/D3bahF.md.jpg" alt="D3bahF.md.jpg"></a></p>
<h2 id="C的编译与链接"><a href="#C的编译与链接" class="headerlink" title="C的编译与链接"></a>C的编译与链接</h2><p><a target="_blank" rel="noopener external nofollow noreferrer" href="https://blog.csdn.net/ios_xumin/article/details/73457394">摘自CSDN</a></p>
<h3 id="编译器"><a href="#编译器" class="headerlink" title="编译器"></a>编译器</h3><p> 编译器的作用是将保存着我们所写代码的源文件，转换成一种称为目标文件的二进制文件。源文件是我们能看懂的，是给我们用的。而二进制文件是计算机能看懂的文件，是给计算机用的。计算机根据二进制文件中的内容决定该做什么事情，不该做什么事情。</p>
<p> 在这个转换过程中，编译器先对源文件中的内容进行扫描，根据C语言的语法要求，逐个检查源文件中出现的每一个字母或者文字。</p>
<p>  如果这些文字符合C语言的语法要求，那么它就根据这些字母和文字的含义将其转换成计算机可以识别的二进制代码，并将其按照一定的格式保存在二进制文件中。如果某些地方的字母或者文字不符合C语言的语法要求，那么编译器将报告所有不符合的地方，不再生成二进制文件，只有改正所有不符合语法要求的地方，让编译器重新对改正的源文件进行转化，才可以生成二进制文件。</p>
<p> 编译器对源文件的转换过程在计算机中有个专业的名字，叫”编译”。编译器也因此得名，表示编译的工具。</p>
<h3 id="链接器"><a href="#链接器" class="headerlink" title="链接器"></a>链接器</h3><p> 一般所写的程序最终是要运行在某个操作系统上的。因此，即使是一个很简单的程序也需要操作系统来处理很多事情，才能使程序正常运行。操作系统往往会提供一些被称为开发库的二进制文件，编译器产生的目标二进制文件只有和这些库二进制文件结合才能生成一个可执行程序，才能使我们写的程序正常地运行于某个操作系统之上。</p>
<p>  另外，有的时候我们可能会开发一些专业的或者功能很复杂的软件，这类软件要从头做，往往很麻烦。这个时候，就得看看其他公司或者业界有没有提供此类功能实现，可以买过来使用。往往买过来的也是一堆库二进制文件，只有把这些库二进制文件和编译器产生的目标二进制文件结合起来才能产生需要的可执行程序。</p>
<p> 链接器所做的工作就是将所有的二进制文件链接起来融合成一个可执行程序，不管这些二进制文件是目标二进制文件还是库二进制文件。链接器将二进制文件融合的这一过程，在计算机中也有一个专业的名字–”链接”，链接器也因此得名，表示链接的工具</p>
<h2 id="基本输入与输出"><a href="#基本输入与输出" class="headerlink" title="基本输入与输出"></a>基本输入与输出</h2><h3 id="论scanf的安全性"><a href="#论scanf的安全性" class="headerlink" title="论scanf的安全性"></a>论scanf的安全性</h3><p>微软那边用scanf_s解决了，但这种情况（弄出stack overflow）百试不厌</p>
<p>详情可以看这个</p>
<p><a target="_blank" rel="noopener external nofollow noreferrer" href="https://www.freebuf.com/articles/system/253225.html">https://www.freebuf.com/articles/system/253225.html</a></p>
<p>长见识了</p>
<h3 id="scanf"><a href="#scanf" class="headerlink" title="scanf"></a>scanf</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">scanf</span>(<span class="string">&quot;%d,%d&quot;</span>,&amp;a,&amp;b);</span><br><span class="line"><span class="built_in">scanf</span>(<span class="string">&quot;%s&quot;</span>,str) <span class="comment">/*字符组不能加&amp;地址符*/</span></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">&quot;Max is %d&quot;</span>,a);</span><br></pre></td></tr></table></figure>

<p><strong>Scanf输入的是地址，必须有&amp;(取地址符)，如果是指向数据的指针则不用加</strong></p>
<p>负号也算一位</p>
<p>%f 默认是输出6位</p>
<p>%3f 输出3位，不足前面补0</p>
<p>%.3 小数点后输出三位，不足后面补0</p>
<p>1e6  10的6次方</p>
<p><strong>没有输出二进制的占位符</strong></p>
<p><strong>指数是实型</strong></p>
<h3 id="占位符"><a href="#占位符" class="headerlink" title="占位符"></a>占位符</h3><p>%p  指针地址  %o 八进制 %x 十六进制</p>
<p>%c  输出单个字符  %ld 长整型  %nd输出几位的整型</p>
<p>%s  输出字符串</p>
<p>输入时的时候要注意</p>
<p>%f 单精度（float） </p>
<p>%lf 双精度（double）  提示：5.0&#x2F;2.0</p>
<p>%lld long long int</p>
<p>%b 布尔型（cpp中使用）</p>
<p><strong>Attention！！！</strong></p>
<p><strong>回车也算字符！！！！</strong></p>
<p>一般用getchar()吸掉</p>
<p>要是写上%3d，输出的数字将占3个空</p>
<h3 id="C语言各数据类型所占字节数"><a href="#C语言各数据类型所占字节数" class="headerlink" title="C语言各数据类型所占字节数"></a>C语言各数据类型所占字节数</h3><p>1字节是8位</p>
<ul>
<li>char存储大小1字节，值范围-128~127；</li>
<li>unsigned char存储大小1字节，值范围0~255；</li>
<li>short存储大小2字节，值范围-32768~32767；</li>
<li>unsigned short存储大小2字节，值范围0~65535；</li>
<li>int——</li>
</ul>
<blockquote>
<p>​       16位系统存储大小2字节，值范围-32768<del>32767，<br>​       32、64位系统存储大小4字节，值范围-2147483648</del>2147483647；</p>
</blockquote>
<p>10位数</p>
<ul>
<li>unsigned int——</li>
</ul>
<blockquote>
<p>​       16位系统存储大小2字节，值范围0<del>65535，<br>​       32、64位系统存储大小4字节，值范围0</del>4294967295；</p>
</blockquote>
<ul>
<li>long——</li>
</ul>
<blockquote>
<p>​       16、32位系统存储大小4字节，值范围-2147483648<del>2147483647，<br>​       64位系统存储大小8字节，值范围-9223372036854775808</del>9223372036854775807；</p>
</blockquote>
<p>VC中long int 和 int是没有区别的，两种类型均用4个字节存放数据。</p>
<ul>
<li>unsigned long——</li>
</ul>
<blockquote>
<p>​       16、32位系统存储大小4字节，值范围0<del>4294967295，<br>​       64位系统存储大小8字节，值范围0</del>18446744073709551615；</p>
</blockquote>
<ul>
<li><p>long long的最大值：9223372036854775807<br>long long的最小值：-9223372036854775808 </p>
<p>共19位数</p>
</li>
<li><p>float存储大小4字节，值范围1.175494351<em>10^-38~3.402823466</em>10^38；</p>
</li>
<li><p>double存储大小8字节，值范围2.2250738585072014<em>10^-308~1.7976931348623158</em>10^308；</p>
</li>
<li><p>long long存储大小8字节，值范围-9223372036854775808~9223372036854775807；</p>
</li>
<li><p>unsigned long long存储大小8字节，值范围0~18446744073709551615；</p>
</li>
<li><p>long double——</p>
</li>
</ul>
<blockquote>
<p>​       16位系统存储大小8字节，值范围2.22507<em>10^-308~1.79769</em>10^308，<br>​       32位系统存储大小12字节（有效位10字节，为了对齐实际分配12字节），值范围3.4<em>10^-4932 到 1.1</em>10^4932，<br>​       64位系统存储大小16字节（有效位10字节，为了对齐实际分配16字节），值范围3.4<em>10^-4932 到 1.1</em>10^4932；</p>
</blockquote>
<ul>
<li>指针——</li>
</ul>
<blockquote>
<p>​       16位系统存储大小2字节，<br>​       32位系统存储大小4字节，<br>​       64位系统存储大小8字节。</p>
</blockquote>
<p>C51 sfr 1字节</p>
<p>bit 1个位</p>
<p>\n是换行  \r回车  \t应该是空格</p>
<p><a target="_blank" rel="noopener external nofollow noreferrer" href="https://zhuanlan.zhihu.com/p/98674721">摘自知乎</a></p>
<h3 id="C的强制类型转换"><a href="#C的强制类型转换" class="headerlink" title="C的强制类型转换"></a>C的强制类型转换</h3><p>强制类型转换是把变量从一种类型转换为另一种数据类型。例如，如果您想存储一个 long 类型的值到一个简单的整型中，您需要把 long 类型强制转换为 int 类型。您可以使用<strong>强制类型转换运算符</strong>来把值显式地从一种类型转换为另一种类型，如下所示：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(type_name) expression</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">float a=3.124;</span><br><span class="line">a=(int)a;</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line">#include &lt;math.h&gt;</span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	float a=3.124;</span><br><span class="line">	int b=0;</span><br><span class="line">    b=(int)a;</span><br><span class="line">    printf(&quot;%d&quot;,b);</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="小写转大写"><a href="#小写转大写" class="headerlink" title="小写转大写"></a>小写转大写</h3><p>由于大写字母与小写字母之间的<strong>差值为 32</strong>，因此小写字母转换为大写字母的方法就是将小写字母的 ASCII 码值减去 32，便可得到与之对应的大写字母。</p>
<p>正规方法使用 toupper()， tolower() 函数</p>
<p>需要ctype.h</p>
<h4 id="Tip"><a href="#Tip" class="headerlink" title="Tip"></a>Tip</h4><p>scanf(“%s”,s); &#x2F;&#x2F;采用bai这种方法不能输入空格，空格作为字符串结束符。<br>gets(s); &#x2F;&#x2F;采用这种方法可以输入空格，回车作为字符串结束符。 </p>
<h3 id="Exit"><a href="#Exit" class="headerlink" title="Exit"></a>Exit</h3><p>需要stdlib.h</p>
<h3 id="转义字符"><a href="#转义字符" class="headerlink" title="转义字符"></a>转义字符</h3><p>\b 名义上是回车，实际上是光标右移</p>
<p>\t tab</p>
<h3 id="基本运算"><a href="#基本运算" class="headerlink" title="基本运算"></a>基本运算</h3><p>乘方有时可用^,但正确做法应该是pow()</p>
<h4 id="取模运算"><a href="#取模运算" class="headerlink" title="取模运算"></a>取模运算</h4><p>运算法则如下</p>
<blockquote>
<ol>
<li>(a + b) % p &#x3D; (a % p + b % p) % p （1）</li>
<li>(a - b) % p &#x3D; (a % p - b % p ) % p （2）</li>
<li><strong>(a * b) % p &#x3D; (a % p * b % p) % p （3）</strong></li>
</ol>
</blockquote>
<h4 id="乘方运算"><a href="#乘方运算" class="headerlink" title="乘方运算"></a>乘方运算</h4><p>pow(),在math.h,平方运算</p>
<p>要不然就是*N次，手动写和循环都行</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=power;i++)&#123;</span><br><span class="line">    result=result*base;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<h3 id="Tip：小数存储"><a href="#Tip：小数存储" class="headerlink" title="Tip：小数存储"></a>Tip：小数存储</h3><p>十进制（转二进制），小数点后面乘2，取整数位的0和1</p>
<p>即可能会存在精度损失</p>
<h3 id="i"><a href="#i" class="headerlink" title="i++"></a>i++</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">x=i++等价于x=i,i=i+<span class="number">1</span>,懂？</span><br></pre></td></tr></table></figure>

<p>i++是后缀递增的意思，i++是先进行表达式运算，再进行自增运算。把i++的运算过程拆分开，等效于i&#x3D;i+1可以看到运算结果是一致的</p>
<p>++i是前缀递增的意思，++i是先进行自增或者自减运算，再进行表达式运算。运算结果可以发现，仅从i的值来看，++i和i++最终的i值是一样的，都是i自增加了1</p>
<h3 id="常见逻辑"><a href="#常见逻辑" class="headerlink" title="常见逻辑"></a>常见逻辑</h3><p>顺序，选择，循环</p>
<h3 id="注释"><a href="#注释" class="headerlink" title="注释"></a>注释</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*  Hello */</span></span><br></pre></td></tr></table></figure>

<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Hello</span></span><br></pre></td></tr></table></figure>



<h3 id="字符处理"><a href="#字符处理" class="headerlink" title="字符处理"></a>字符处理</h3><h4 id="字符存储"><a href="#字符存储" class="headerlink" title="字符存储"></a>字符存储</h4><p>做大小写混合转换时要注意，配合 while ch!&#x3D;’\n’ 进行判断</p>
<p>ASCII，大写A为65，小写为97</p>
<p>做题时注意，小写的ascii码要更大</p>
<p>为什么是32不是26？</p>
<p>原因：26个小写和26个大写之间插入了6个字符</p>
<h4 id="常规处理"><a href="#常规处理" class="headerlink" title="常规处理"></a>常规处理</h4><p>如果你有一长串字符，却用</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">scanf</span>(<span class="string">&quot;%c&quot;</span>,&amp;a);</span><br><span class="line">or a=gecthar;</span><br></pre></td></tr></table></figure>

<p>就可以实现单个输出与转换</p>
<p>即做题时，可以这样直接输出，就省去了使用字符数组的必要</p>
<p>但要实现存储，需要使用下面的方案</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"> </span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span> <span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">   <span class="type">char</span> str1[<span class="number">12</span>] = <span class="string">&quot;Hello&quot;</span>;</span><br><span class="line">   <span class="type">char</span> str2[<span class="number">12</span>] = <span class="string">&quot;World&quot;</span>;</span><br><span class="line">   <span class="type">char</span> str3[<span class="number">12</span>];</span><br><span class="line">   <span class="type">int</span>  len ;</span><br><span class="line"> </span><br><span class="line">   <span class="comment">/* 复制 str1 到 str3 */</span></span><br><span class="line">   <span class="built_in">strcpy</span>(str3, str1);</span><br><span class="line">   <span class="built_in">printf</span>(<span class="string">&quot;strcpy( str3, str1) :  %s\n&quot;</span>, str3 );</span><br><span class="line"> </span><br><span class="line">   <span class="comment">/* 连接 str1 和 str2 */</span></span><br><span class="line">   <span class="built_in">strcat</span>( str1, str2);</span><br><span class="line">   <span class="built_in">printf</span>(<span class="string">&quot;strcat( str1, str2):   %s\n&quot;</span>, str1 );</span><br><span class="line"> </span><br><span class="line">   <span class="comment">/* 连接后，str1 的总长度 */</span></span><br><span class="line">   len = <span class="built_in">strlen</span>(str1);</span><br><span class="line">   <span class="built_in">printf</span>(<span class="string">&quot;strlen(str1) :  %d\n&quot;</span>, len );</span><br><span class="line"> </span><br><span class="line">   <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h4 id="字符数组"><a href="#字符数组" class="headerlink" title="字符数组"></a>字符数组</h4><p><strong>数组名就是指针名！！</strong></p>
<p>\0为占位符</p>
<p>字符数组初始化：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">char</span> a[]=&#123;<span class="string">&#x27;a&#x27;</span>&#125;</span><br></pre></td></tr></table></figure>

<p>getchar()获取单个字符</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">char</span> c;</span><br><span class="line">c=getchar();</span><br></pre></td></tr></table></figure>

<p>一般用循环不断输入单个字符</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">char</span> str[<span class="number">20</span>]=&#123;<span class="number">0</span>&#125;;  <span class="comment">//如果没有定义长度，scanf会出错</span></span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%s&quot;</span>,&amp;str);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%s&quot;</span>,str);</span><br></pre></td></tr></table></figure>

<p>单个字符用单引号，字符串用双引号</p>
<p>使用下列函数时，最好添加头文件&lt;string.h&gt;</p>
<p>gets() 输入字符 （GCC会报警，<strong>不能使用指针</strong>）</p>
<p>puts() 输出字符</p>
<p>strcat(字符数组1,字符数组2)  &#x2F;&#x2F;连接两个字符数组</p>
<p>strcpy（字符数组1,字符串&#x2F;字符数组2） </p>
<p>在VC上会出错</p>
<p><a target="_blank" rel="noopener external nofollow noreferrer" href="https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-3-c4996?redirectedfrom=MSDN&view=vs-2019">MSDN</a>和<a target="_blank" rel="noopener external nofollow noreferrer" href="https://www.zhihu.com/question/31378813">知乎</a>的解决方案</p>
<p>建议使用下面替代</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">strcpy_s(str, <span class="keyword">sizeof</span>(str), str1)</span><br></pre></td></tr></table></figure>

<p>有理由怀疑这是C++的语法，不知道能不能在GCC上用</p>
<p>好像是C11标准</p>
<p>strcmp () 两个字符串进行比较（比较字符是否完全相同）</p>
<p>strlen() 比较字符长度（不包括占位符）</p>
<p>strlwr 转小写（lowercase）</p>
<p>stlupr 转大写 （uppercase）</p>
<p><strong>strlwr和strupr不是标准C库函数，只能在VC中使用。linux gcc环境下需要自行定义这个函数。</strong></p>
<p><strong>可以存在字符二维数组</strong></p>
<p>可以根据空格数判断词数</p>
<p>*<em>注意，形参只能传递指针，不能将数组的所有元素一次性全部传递，除了标准的char <em>p外，还可以将形参写成char c[]。</em></em></p>
<h2 id="C的数组"><a href="#C的数组" class="headerlink" title="C的数组"></a>C的数组</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">type arrayName [ x ][ y ];   /声明一个 x 行 y 列的二维整型数组/</span><br><span class="line">a[<span class="number">3</span>][<span class="number">4</span>] 就是<span class="number">3</span>行<span class="number">4</span>列</span><br></pre></td></tr></table></figure>



<p>只有codeblock在数组初始化不足时会自动补0？？？</p>
<p>二维数组行可以少，但列不行</p>
<h3 id="数组初始化"><a href="#数组初始化" class="headerlink" title="数组初始化"></a>数组初始化</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">memset</span>(a,<span class="number">0</span>,<span class="keyword">sizeof</span>(a));</span><br></pre></td></tr></table></figure>

<p>使a数组中的全部元素全部初始化为0</p>
<p>需要头文件string.h</p>
<p>根据空间大小，从前往后初始化</p>
<p>二维数组也可以</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> a[<span class="number">2</span>][<span class="number">2</span>];</span><br><span class="line">    <span class="built_in">memset</span>(a,<span class="number">0</span>,<span class="keyword">sizeof</span>(a));</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;<span class="number">2</span>;i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> j=<span class="number">0</span>;j&lt;<span class="number">2</span>;j++)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;%d&quot;</span>,a[i][j]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="C的常量"><a href="#C的常量" class="headerlink" title="C的常量"></a>C的常量</h2><p>#define 是宏定义，它不能定义常量，但宏定义可以实现在字面意义上和其它定义常量相同的功能，本质的区别就在于 #define 不为宏名分配内存，而 const 也不为常量分配内存，怎么回事呢，其实 const 并不是去定义一个常量，而是去改变一个变量的存储类，把该变量所占的内存变为只读！</p>
<p><strong>const</strong> 定义的是变量不是常量，只是这个变量的值不允许改变是常变量！带有类型。编译运行的时候起作用存在类型检查。</p>
<p><strong>define</strong> 定义的是不带类型的常数，只进行简单的字符替换。在预编译的时候起作用，不存在类型检查。</p>
<h2 id="C的存储类"><a href="#C的存储类" class="headerlink" title="C的存储类"></a>C的存储类</h2><p>使用 static 修饰局部变量可以在函数调用之间保持局部变量的值。</p>
<p>static 修饰符也可以应用于全局变量</p>
<p><em>当 static 修饰全局变量时，会使变量的作用域限制在声明它的文件内。</em></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">age</span><span class="params">()</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=<span class="number">5</span>;i++)&#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%d!=%d\n&quot;</span>,i,age(i));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">int</span> <span class="title function_">age</span><span class="params">(<span class="type">int</span> t)</span>&#123;</span><br><span class="line">    <span class="type">static</span> <span class="type">int</span> f=<span class="number">1</span>; <span class="comment">//static在局部变量的用处</span></span><br><span class="line">    f*=t;</span><br><span class="line">    <span class="keyword">return</span> (f);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="register-寄存器变量"><a href="#register-寄存器变量" class="headerlink" title="register 寄存器变量"></a>register 寄存器变量</h3><p><strong>register</strong> 存储类用于定义存储在寄存器中而不是 RAM 中的局部变量。这意味着变量的最大尺寸等于寄存器的大小（通常是一个词），且不能对它应用一元的 ‘&amp;’ 运算符（因为它没有内存位置）。</p>
<p>这货比内存快很多，好不好？</p>
<p>register<strong>变量必须是能被CPU所接受的类型</strong>。这通常意味着register变量<strong>必须是一个单个的值</strong>，并且<strong>长度应该小于或者等于整型的长度</strong>。不过，有些机器的寄存器也能存放浮点数</p>
<p><strong>在某些情况下，把变量保存在寄存器中反而会降低程序的运行速度</strong></p>
<p>因为被占用的寄存器不能再用于其它目的；或者变量被使用的次数不够多，不足以装入和存储变量所带来的额外开销</p>
<p>随着编译程序设计技术的进步，在决定那些变量应该被存到寄存器中时，现在的C编译环境能比程序员做出更好的决定。实际上，许多编译程序都会忽略，它仅仅<strong>是暗示而不是命令</strong></p>
<p>只有<strong>局部自动与行参</strong>可作为寄存器变量</p>
<p>使用extern引用另一个文件的全局变量</p>
<p>函数定义时省略extern，则默认为外部函数</p>
<h2 id="C的运算"><a href="#C的运算" class="headerlink" title="C的运算"></a>C的运算</h2><p>int除完还是整数，即使最终结果是输出到float的变量</p>
<h3 id="除以0的故事"><a href="#除以0的故事" class="headerlink" title="除以0的故事"></a>除以0的故事</h3><p>整数除0会报错，但是浮点数不会（会返回inf infinety）</p>
<p>java也是这样，我觉得原因应该一样</p>
<p><strong>因为java的float和double使用了IEEE 754标准。</strong></p>
<p><strong>这个标准规定：浮点数除以0等于正无穷或负无穷。</strong></p>
<p><strong>正无穷大</strong>的定义居然是<strong>1.0f&#x2F;0.0f</strong> 。<strong>负无穷大</strong>的定义为**-1.0f&#x2F;0.0f**，非数的定义为0.0f&#x2F;0.0f</p>
<p>（排除错误时使用，浮点型的情况编译器不会警告）</p>
<h3 id="无限不循环小数"><a href="#无限不循环小数" class="headerlink" title="无限不循环小数"></a>无限不循环小数</h3><p>机器的运算精度有上限，高精度计算需要其他科学方法</p>
<h2 id="C循环"><a href="#C循环" class="headerlink" title="C循环"></a>C循环</h2><p>我认为，在知道循环次数的条件下，使用for循环</p>
<p>while循环在初始不知道循环的情况下使用</p>
<p>不要干些傻事，循环里声明的变量，如果没有初始化，不会因为循环导致变量清0</p>
<p>默认使用i，j,k作为循环变量进行控制</p>
<h3 id="for循环"><a href="#for循环" class="headerlink" title="for循环"></a>for循环</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> ( init; condition; increment )</span><br><span class="line">&#123;</span><br><span class="line">   statement(s);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>init</strong> 会首先被执行，且只会执行一次。这一步允许您声明并初始化任何循环控制变量。<strong>您也可以不在这里写任何语句</strong>，只要有一个分号出现即可。</p>
<p>建议多少到多少时使用</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="type">int</span>=<span class="number">0</span>;i&lt;<span class="number">3</span>;i++)</span><br></pre></td></tr></table></figure>

<p>这个就是循环三次</p>
<p>0，1，2</p>
<h3 id="while循环"><a href="#while循环" class="headerlink" title="while循环"></a>while循环</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span>(condition)</span><br><span class="line">&#123;</span><br><span class="line">   statement(s);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="do-while循环"><a href="#do-while循环" class="headerlink" title="do-while循环"></a>do-while循环</h3><p>do 先进入 while 非0，继续循环</p>
<p>即不管是否满足条件，都要先执行一便循环体</p>
<h3 id="brake"><a href="#brake" class="headerlink" title="brake"></a>brake</h3><p>跳出循环，亦可以跳出switch</p>
<p>（考试时被坑了，谁没事干用switch做判断）</p>
<h3 id="continue"><a href="#continue" class="headerlink" title="continue"></a>continue</h3><p>终止本次循环，经行下一次循环</p>
<h2 id="C判断"><a href="#C判断" class="headerlink" title="C判断"></a>C判断</h2><h3 id="if的语法"><a href="#if的语法" class="headerlink" title="if的语法"></a>if的语法</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>(boolean_expression)</span><br><span class="line">&#123;</span><br><span class="line">   <span class="comment">/* 如果布尔表达式为真将执行的语句 */</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">&#123;</span><br><span class="line">   <span class="comment">/* 如果布尔表达式为假将执行的语句 */</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>虽说可以用缩进代替大括号，不推荐这么做</p>
<p>if(i–)</p>
<p>if判断完后，马上–,然后再执行框内</p>
<h3 id="switch的语法"><a href="#switch的语法" class="headerlink" title="switch的语法"></a>switch的语法</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">switch</span>(grade)</span><br><span class="line">   &#123;</span><br><span class="line">   <span class="keyword">case</span> <span class="string">&#x27;A&#x27;</span> :</span><br><span class="line">      <span class="built_in">printf</span>(<span class="string">&quot;很棒！\n&quot;</span> );</span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">   <span class="keyword">case</span> <span class="string">&#x27;B&#x27;</span> :</span><br><span class="line">   <span class="keyword">case</span> <span class="string">&#x27;C&#x27;</span> :</span><br><span class="line">      <span class="built_in">printf</span>(<span class="string">&quot;做得好\n&quot;</span> );</span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">   <span class="keyword">case</span> <span class="string">&#x27;D&#x27;</span> :</span><br><span class="line">      <span class="built_in">printf</span>(<span class="string">&quot;您通过了\n&quot;</span> );</span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">   <span class="keyword">case</span> <span class="string">&#x27;F&#x27;</span> :</span><br><span class="line">      <span class="built_in">printf</span>(<span class="string">&quot;最好再试一下\n&quot;</span> );</span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">   <span class="keyword">default</span> :</span><br><span class="line">      <span class="built_in">printf</span>(<span class="string">&quot;无效的成绩\n&quot;</span> );</span><br><span class="line">   &#125;</span><br><span class="line">   <span class="built_in">printf</span>(<span class="string">&quot;您的成绩是 %c\n&quot;</span>, grade );</span><br></pre></td></tr></table></figure>

<p>就是垃圾玩意</p>
<p>每个判断后面都要加break</p>
<p>可以连写，比如caseA与caseB共通条件</p>
<p>这TM问题很严重</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> c;</span><br><span class="line">	<span class="keyword">while</span>((c=getchar())!=<span class="string">&#x27;\n&#x27;</span>)</span><br><span class="line">		<span class="keyword">switch</span>(c-<span class="string">&#x27;2&#x27;</span>)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">case</span> <span class="number">0</span>:</span><br><span class="line">			<span class="keyword">case</span> <span class="number">1</span>:<span class="built_in">putchar</span>(c+<span class="number">4</span>);</span><br><span class="line">			<span class="keyword">case</span> <span class="number">2</span>:<span class="built_in">putchar</span>(c+<span class="number">4</span>);<span class="keyword">break</span>;</span><br><span class="line">			<span class="keyword">case</span> <span class="number">3</span>:<span class="built_in">putchar</span>(c+<span class="number">3</span>);</span><br><span class="line">			<span class="keyword">default</span>:<span class="built_in">putchar</span>(c+<span class="number">2</span>);<span class="keyword">break</span>;</span><br><span class="line">		&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//这输出的都是什么好东西</span></span><br><span class="line"><span class="comment">//要是进0，012全部执行</span></span><br></pre></td></tr></table></figure>



<h3 id="do-while-循环"><a href="#do-while-循环" class="headerlink" title="do while 循环"></a>do while 循环</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">do</span><br><span class="line">&#123;</span><br><span class="line">   statement(s);</span><br><span class="line"></span><br><span class="line">&#125;while( condition );</span><br></pre></td></tr></table></figure>

<p>**如果条件为真，控制流会跳转回上面的 do，然后重新执行循环中的 statement(s)**。这个过程会不断重复，直到给定条件变为假为止。</p>
<h3 id="else"><a href="#else" class="headerlink" title="else"></a>else</h3><p>else与最近的if匹配</p>
<h3 id="运算符-三元运算符"><a href="#运算符-三元运算符" class="headerlink" title="? : 运算符(三元运算符)"></a>? : 运算符(三元运算符)</h3><p>我们已经在前面的章节中讲解了 **条件运算符 ? :**，可以用来替代 <strong>if…else</strong> 语句。它的一般形式如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Exp1 ? Exp2 : Exp3;</span><br></pre></td></tr></table></figure>

<p>其中，Exp1、Exp2 和 Exp3 是表达式。请注意，冒号的使用和位置。</p>
<p>? 表达式的值是由 Exp1 决定的。如果 Exp1 为真，则计算 Exp2 的值，结果即为整个 ? 表达式的值。如果 Exp1 为假，则计算 Exp3 的值，结果即为整个 ? 表达式的值。</p>
<p><strong>Exp1、Exp2 和 Exp3 都必须要有返回值</strong></p>
<h3 id="逻辑运算符"><a href="#逻辑运算符" class="headerlink" title="逻辑运算符"></a>逻辑运算符</h3><p>下表显示了 C 语言支持的所有关系逻辑运算符。假设变量 <strong>A</strong> 的值为 1，变量 <strong>B</strong> 的值为 0，则：</p>
<table>
<thead>
<tr>
<th>运算符</th>
<th>描述</th>
<th>实例</th>
</tr>
</thead>
<tbody><tr>
<td>&amp;&amp;</td>
<td>称为<strong>逻辑与</strong>运算符。如果两个操作数同时都非零，则条件为真。（也就是数学中的且）</td>
<td>(A &amp;&amp; B) 为假。</td>
</tr>
<tr>
<td>||</td>
<td>称为<strong>逻辑或</strong>运算符。如果两个操作数中有任意一个非零，则条件为真。</td>
<td>(A || B) 为真。</td>
</tr>
<tr>
<td>!</td>
<td>称为<strong>逻辑非</strong>运算符。用来逆转操作数的逻辑状态。如果条件为真则逻辑非运算符将使其为假。</td>
<td>!(A &amp;&amp; B) 为真。</td>
</tr>
</tbody></table>
<h2 id="C的函数"><a href="#C的函数" class="headerlink" title="C的函数"></a>C的函数</h2><p><em>不推荐隐式声明</em></p>
<p>无参函数，如print</p>
<p>有参函数，主函数与被调用函数之间存在参数传递</p>
<p>有参函数分为两类</p>
<p><strong>形式参数：定义的变量&#x2F;值</strong></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">age(<span class="type">int</span> a)&#123;</span><br><span class="line">	<span class="comment">/*定义方式*/</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>实际参数：传进来的变量&#x2F;值</strong></p>
<p>预先声明不需要确定数据类型</p>
<p>return后面可以是表达式</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">return</span>(x&gt;y?x:y);</span><br></pre></td></tr></table></figure>



<p>void函数体里面也可以有return，但return后面不需要接任何东西</p>
<h3 id="基础概念"><a href="#基础概念" class="headerlink" title="基础概念"></a>基础概念</h3><p>函数的调用就是stack的使用，参数的进栈跟出栈，还有就是保留原来程序指针位置，这个就是函数的调用<br>当函数调用完毕，他会返回到原来的那个运行指针处<br>所以要保留原来的程序指针位置</p>
<p>原来的程序指针位置，也就是地址也会被放进stack当中</p>
<p>(要讲啥，忘了)</p>
<h3 id="C的递归"><a href="#C的递归" class="headerlink" title="C的递归"></a>C的递归</h3><p>递归指的是在函数的定义中使用函数自身的方法</p>
<p>c允许自己调用自己</p>
<p>函数传递是用过ax寄存器实现</p>
<p>在函数里写上判断条件，符合就返回1，不符合就继续递归</p>
<p><strong>非常重要！！！</strong></p>
<p>因为递归只能用函数实现</p>
<p>以n！为例</p>
<p>n&#x3D;1为1</p>
<p>n！&#x3D; 1时继续递归</p>
<h2 id="C的指针"><a href="#C的指针" class="headerlink" title="C的指针"></a>C的指针</h2><p>这图话里有话</p>
<p><a target="_blank" rel="noopener external nofollow noreferrer" href="https://imgchr.com/i/DsXNDJ"><img src="https://s3.ax1x.com/2020/11/28/DsXNDJ.md.jpg" alt="DsXNDJ.md.jpg"></a></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">ip = &amp;var;  <span class="comment">/* 在指针变量中存储 var 的地址 */</span></span><br><span class="line"><span class="type">int</span>  *ptr = <span class="literal">NULL</span> <span class="comment">/*空指针*/</span></span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">if(ptr)     /* 如果 p 非空，则完成 */</span><br><span class="line">if(!ptr)    /* 如果 p 为空，则完成 */</span><br></pre></td></tr></table></figure>

<p>&amp;表示取地址，*表示取数据</p>
<p>*<strong>表示指针变量，但指针变量名为ptr</strong></p>
<p><strong>一个指针变量只能储存一个地址</strong></p>
<p>编译器对地址运算不做越界检查，程序员要小心谨慎地移动指针，时刻避免越界</p>
<p>指针变量是有空间的，存放的是另外一个变量的地址</p>
<p>&amp;为取地址</p>
<p>*p为指针变量指向的变量的值（规范表述）</p>
<p>int一个是4位，char一个是8位</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a[<span class="number">5</span>]=&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>&#125;;</span><br><span class="line">  <span class="type">int</span> *i;</span><br><span class="line">  i=a;</span><br><span class="line">  <span class="keyword">for</span>(<span class="type">int</span> c=<span class="number">0</span>;c&lt;<span class="number">5</span>;c++)&#123;</span><br><span class="line">     <span class="built_in">printf</span>(<span class="string">&quot;%d \n&quot;</span>,*(i+c));</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>

<p>i&#x3D;a等效于i&#x3D;&amp;a[0]</p>
<p>C语言规定:<strong>数组名</strong>代表<strong>数组的首地址</strong></p>
<p><del>(注意一下，这个例子里，不能直接对a操作，只能对传完的指针进行指针运算)</del> Quite Wrong</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> a[<span class="number">4</span>];</span><br><span class="line">    <span class="built_in">memset</span>(a,<span class="number">0</span>,<span class="keyword">sizeof</span>(a));</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;<span class="number">4</span>;i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>,*(a+i));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>数组名做函数参数时，把实参数组的地址传给了形参</p>
<p><strong>实现改变形参的同时改变实参</strong></p>
<p>Attention：<u>实参变量与形参变量的数据传递是单向的值传递</u></p>
<p>“调用函数不能改变<strong>实参指针变量的值</strong></p>
<p>但可以改变<strong>实参指针变量所指变量的值</strong>”</p>
<p>数组中，(米字号)a等效于a[0]，*(a+1)等效于a[1]</p>
<p>C 指针是一个用数值表示的地址。因此，您可以对指针执行算术运算。可以对指针进行四种算术运算：++、–、+、-</p>
<p><strong>指针可以递增</strong>（可用来输出数组）</p>
<p>可以有数组指针</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">*(p++)与*(++p)的区别</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">*p++等价于*(p++)</span><br></pre></td></tr></table></figure>

<p>前者先输出p，再p+1</p>
<p>后者先p+1,再输出p</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">main</span> <span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="type">int</span> a=<span class="number">5</span>;</span><br><span class="line">  <span class="type">int</span> *i;</span><br><span class="line">  i=&amp;a;</span><br><span class="line">  (*i)+=<span class="number">1</span>;</span><br><span class="line">  <span class="built_in">printf</span>(<span class="string">&quot;%d&quot;</span>,a);</span><br><span class="line">  <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>指针变量i具有a的地址，*i等效于a</p>
<p><del>指针做循环条件时，不能用scanf</del>（理解有误）</p>
<p><strong>数组的本质就是指针</strong></p>
<h3 id="二维数组指针"><a href="#二维数组指针" class="headerlink" title="二维数组指针"></a>二维数组指针</h3><p>在这里，*<strong>a取的不是值，是指针</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">*a与a[0]地址相等,即第一行，这是一个指针（不等价，因为步进值不同）</span><br></pre></td></tr></table></figure>

<p>a表示第一行，第一列（a00）</p>
<p>a+1表示第二行，第一列（a10）</p>
<p>对于一个a32数组来说，a+3会输出第二行，第一列的位置，<strong>仅C语言可以</strong>（属于越界行为，不推荐，如果你使用其他语言很有可能出错【）</p>
<p>a0+1第一行，第二列（a01）</p>
<p>*p[4] 是二维数组</p>
<p>**p类似于二维数组（指针变量）</p>
<p>对比于一维数组a[5]</p>
<p>a+1是地址，a[1]是元素</p>
<p>下面给个案例，大家自己体会</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> a[<span class="number">2</span>][<span class="number">2</span>];</span><br><span class="line">    <span class="type">int</span> *c;</span><br><span class="line">    c=a[<span class="number">0</span>];</span><br><span class="line">    a[<span class="number">0</span>][<span class="number">0</span>]=<span class="number">1</span>;a[<span class="number">0</span>][<span class="number">1</span>]=<span class="number">2</span>;a[<span class="number">1</span>][<span class="number">0</span>]=<span class="number">3</span>;;a[<span class="number">1</span>][<span class="number">1</span>]=<span class="number">4</span>;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">    for(int i=0;i&lt;2;i++)</span></span><br><span class="line"><span class="comment">    &#123;</span></span><br><span class="line"><span class="comment">        for(int j=0;j&lt;2;j++)</span></span><br><span class="line"><span class="comment">        &#123;</span></span><br><span class="line"><span class="comment">            printf(&quot;%d &quot;,a[i][j]);</span></span><br><span class="line"><span class="comment">        &#125;</span></span><br><span class="line"><span class="comment">    &#125;</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;<span class="number">2</span>;i++)</span><br><span class="line">    &#123;</span><br><span class="line">        c=a[i];</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> j=<span class="number">0</span>;j&lt;<span class="number">2</span>;j++)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>,*c);</span><br><span class="line">            c++;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注释内和注释外完全等价</p>
<p>这部分其实很烧脑子</p>
<h3 id="字符数组指针"><a href="#字符数组指针" class="headerlink" title="字符数组指针"></a>字符数组指针</h3><p><strong>字符串常量</strong>按<strong>字符数组</strong>处理</p>
<p>所以%s既能输出字符串，又能输出字符数组指针</p>
<p>输出指针，说明字符串是地址</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">可以<span class="type">char</span> *<span class="built_in">string</span>=“I love China”</span><br><span class="line">等价于<span class="type">char</span> *<span class="built_in">string</span></span><br><span class="line"><span class="built_in">string</span>=<span class="string">&#x27;I love China&#x27;</span></span><br></pre></td></tr></table></figure>

<p><strong>以上做法只是把I love China的首地址赋给了指针变量string</strong></p>
<p>使用%s进行输出</p>
<p>字符变量不能直接赋值</p>
<p>例如</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">char string</span><br><span class="line">string=&#x27;I love China&#x27;是行不通的</span><br><span class="line">string赋得的是首地址</span><br></pre></td></tr></table></figure>

<p>实参与行参可以是<strong>数组名</strong>或<strong>字符指针变量</strong></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"># <span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">test</span><span class="params">()</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">char</span> str[]=<span class="string">&quot;I love China&quot;</span>;</span><br><span class="line">    <span class="type">char</span> *a;</span><br><span class="line">    a=str;</span><br><span class="line">    a+=<span class="number">7</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%s&quot;</span>,a);</span><br><span class="line">&#125;    </span><br></pre></td></tr></table></figure>

<p>可以通过控制指针得到想要的字符串</p>
<p>以此类推，也可以控制指针变量输出单个字符</p>
<h3 id="函数指针"><a href="#函数指针" class="headerlink" title="函数指针"></a>函数指针</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">int max()</span><br><span class="line">int (*p)()</span><br><span class="line">p=max</span><br></pre></td></tr></table></figure>

<p>这就是一个函数指针</p>
<p>其常用的用途是<strong>把函数当作参数传递到其他函数</strong></p>
<p>函数为实参，指针为形参</p>
<h4 id="将数组传递给函数"><a href="#将数组传递给函数" class="headerlink" title="将数组传递给函数"></a>将数组传递给函数</h4><p>以数组元素循环为例进行演示</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> MAXN 100</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">ArrayShift</span><span class="params">(<span class="type">int</span> a[], <span class="type">int</span> n, <span class="type">int</span> m)</span>;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> a[MAXN],n,m;</span><br><span class="line">    <span class="type">int</span> i;</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d%d&quot;</span>,&amp;n,&amp;m);</span><br><span class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;n;i++) <span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;a[i]);</span><br><span class="line">    ArrayShift(a,n,m);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;n;i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span>(i!=<span class="number">0</span>)<span class="built_in">printf</span>(<span class="string">&quot; &quot;</span>);</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%d&quot;</span>,a[i]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">ArrayShift</span><span class="params">(<span class="type">int</span> a[],<span class="type">int</span> n,<span class="type">int</span> m)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">while</span> (m--)<span class="comment">//移动的次数</span></span><br><span class="line">    &#123;</span><br><span class="line">         <span class="type">int</span> temp = a[n - <span class="number">1</span>];<span class="comment">//保留最后一位</span></span><br><span class="line">         <span class="type">int</span> i;</span><br><span class="line">        <span class="keyword">for</span>(i = n - <span class="number">1</span>; i &gt;= <span class="number">1</span>; i--)</span><br><span class="line">        &#123;</span><br><span class="line">            a[i] = a[i - <span class="number">1</span>];</span><br><span class="line">        &#125;</span><br><span class="line">        a[<span class="number">0</span>] = temp;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>





<h4 id="返回指针的函数"><a href="#返回指针的函数" class="headerlink" title="返回指针的函数"></a>返回指针的函数</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> *<span class="title function_">p</span><span class="params">(a，b)</span></span><br></pre></td></tr></table></figure>

<p>即取函数p的指针</p>
<h3 id="指向指针的指针"><a href="#指向指针的指针" class="headerlink" title="指向指针的指针"></a>指向指针的指针</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">char **p</span><br></pre></td></tr></table></figure>

<h3 id="指针数组"><a href="#指针数组" class="headerlink" title="指针数组"></a>指针数组</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int (*p)[3]</span><br></pre></td></tr></table></figure>

<p>要注意，这个指针数组虽然只用3个元素，但由于指向的是地址，地址是没有个数的，所以理论上可以输出所有数</p>
<h3 id="常量指针与指针常量"><a href="#常量指针与指针常量" class="headerlink" title="常量指针与指针常量"></a>常量指针与指针常量</h3><p><a target="_blank" rel="noopener external nofollow noreferrer" href="https://blog.csdn.net/chenjin_zhong/article/details/6097903">https://blog.csdn.net/chenjin_zhong/article/details/6097903</a></p>
<p>常量指针与指针常量是C语言中两个比较容易混淆的概念:</p>
<p>(1) const char* p;</p>
<p>(2) char* const p&#x3D;a;</p>
<p>(3) char* p&#x3D;”abc”;</p>
<p>(1)式定义了一个<strong>常量指针</strong>，即指向一个常量的指针，指向的内容是常量，不可修改，放在常量区的，但指针本身可以修改，即<em>p&#x3D;’b’，是非法的，</em>p是p指向的常量的第一个字符，是个常量，不能改变的。p&#x3D;&amp;q这是可以的，指针可以指向不同的地址。</p>
<p>以下程序非法</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> a[]=&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125;;</span><br><span class="line">    <span class="type">const</span> <span class="type">int</span> *t=a;<span class="comment">//const 去掉即合法</span></span><br><span class="line">    (*t)=(*t)+<span class="number">1</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d&quot;</span>,*t);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>(2)式定义了一个<strong>指针常量</strong>，即指针本身是个常量，不可修改，<strong>但指针指向的内容可以修改</strong>,一开始定义时让它指向数组a. *p&#x3D;’b’这是可以的，但p&#x3D;&amp;b是非法的。</p>
<p>以下程序非法</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> a[]=&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125;;</span><br><span class="line">    <span class="type">int</span>* <span class="type">const</span> t=a;</span><br><span class="line">    t++;<span class="comment">//非法区域</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d&quot;</span>,*t);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>记忆:  const 常量 * 指针，当const 在<em>之前就是常量指针，而const在</em>之后，就是指针常量.</p>
<p> const char<em>p 即char</em>p是个常量，所以内容是常量。</p>
<p> char* const p; 即指针p是个常量。</p>
<p>(3)  char<em>p定义的是一个指针变量p,指向字符串abc的首地址。这里特别要注意，在C语言中，(3)式定义的是一个常量字符串，它被放在静态存储区的常量区进行存储，而p是一个指针变量，放在栈上。如果</em>p&#x3D;’b’,在编译时能通过，但在运行时就会出现错误，因为你试图去改变常量区的内容。</p>
<p>下面程序非法</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">char</span>* t=<span class="string">&quot;ANC&quot;</span>;</span><br><span class="line">    *t =<span class="string">&#x27;b&#x27;</span>;<span class="comment">//非法来源</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%s&quot;</span>,t);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p><strong>常量指针指的是内容不可变，地址可以改变，即指针可以指向别的地址(保护数据不被修改)。而指针常量指的是指针本身不可变，而内容可以修改（固定位置）</strong></p>
<p>避免在参数传递时造成更改，<strong>相当实用</strong></p>
<h3 id="Main的形参-重要彩蛋"><a href="#Main的形参-重要彩蛋" class="headerlink" title="Main的形参(重要彩蛋)"></a>Main的形参(重要彩蛋)</h3><p>指针数组的一个重要应用是作为main的形参</p>
<p>命令行的参数都是<strong>字符串</strong>，字符串首地址构成指针数组</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">main(<span class="type">int</span> argc,<span class="type">char</span> *argv[])&#123;&#125;</span><br></pre></td></tr></table></figure>

<p>argc指命令行中<strong>参数的个数</strong></p>
<p>argv 为<strong>字符串指针组</strong></p>
<p>可能涉及强制转换</p>
<p>字符串转数字需要stdlib.h</p>
<p>下面是一个简易的加法计算</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc,<span class="type">char</span> *argv[])</span>&#123;</span><br><span class="line">    <span class="type">int</span> b=<span class="number">0</span>;</span><br><span class="line">    <span class="keyword">while</span>(argc&gt;<span class="number">1</span>)</span><br><span class="line">    &#123;++argv;</span><br><span class="line">    b=atoi(*argv)+b;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>,b);</span><br><span class="line">    --argc;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="Tips"><a href="#Tips" class="headerlink" title="Tips"></a>Tips</h3><p>若两个指针在同一数组，指针变量可以相加减，也可以进行比较</p>
<h3 id="案例"><a href="#案例" class="headerlink" title="案例"></a>案例</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">int</span> matrix[<span class="number">3</span>][<span class="number">3</span>]=&#123;&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125;,&#123;<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>&#125;,&#123;<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>&#125;&#125;;</span><br><span class="line">    <span class="type">int</span> *a;    <span class="comment">//以一维数组的形式，遍历二维数组</span></span><br><span class="line"> 	<span class="type">int</span> *b[<span class="number">3</span>];    <span class="comment">//存的是地址，指向的是整形数据</span></span><br><span class="line">    <span class="type">int</span> (*b)[<span class="number">3</span>];  <span class="comment">//存的是地址，指向的是地址，一共有三个指针变量,类似于二维数组</span></span><br><span class="line">    </span><br><span class="line">    a=matrix;</span><br><span class="line">    <span class="comment">//一维数组指针可以遍历二维数组指针</span></span><br><span class="line">    </span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%x,%x,%x\n&quot;</span>,a, &amp;matrix[<span class="number">0</span>][<span class="number">0</span>], &amp;matrix[<span class="number">0</span>][<span class="number">1</span>]);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d,%d,%d\n&quot;</span>,*(*a), *(*(a+<span class="number">1</span>)), *(a+<span class="number">3</span>));</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>还是以输出数组为例</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">int</span> matrix[<span class="number">3</span>][<span class="number">3</span>]=&#123;&#123;<span class="number">10</span>,<span class="number">2</span>,<span class="number">3</span>&#125;,&#123;<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>&#125;,&#123;<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>&#125;&#125;;</span><br><span class="line">    <span class="type">int</span> *a=<span class="literal">NULL</span>;</span><br><span class="line">    <span class="type">int</span> *c=<span class="literal">NULL</span>; </span><br><span class="line">    a=matrix[<span class="number">0</span>]+<span class="number">4</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;<span class="number">5</span>;i++)&#123;</span><br><span class="line">        <span class="type">int</span> b=<span class="number">0</span>;</span><br><span class="line">        b=*(a+i);</span><br><span class="line">        *(a+i)=*(a-i);</span><br><span class="line">        *(a-i)=b;</span><br><span class="line">    &#125;</span><br><span class="line">    a-=<span class="number">4</span>;</span><br><span class="line">    c=a;</span><br><span class="line">    <span class="keyword">for</span>(;a&lt;(c+<span class="number">9</span>);a++)&#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%d \n&quot;</span>,*a);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">int</span> matrix[<span class="number">3</span>][<span class="number">3</span>]=&#123;&#123;<span class="number">10</span>,<span class="number">2</span>,<span class="number">3</span>&#125;,&#123;<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>&#125;,&#123;<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>&#125;&#125;;</span><br><span class="line">    <span class="type">int</span> (*a)[<span class="number">3</span>];</span><br><span class="line">    a=matrix;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;<span class="number">3</span>;i++)&#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%d \n&quot;</span>,*(*(a+i)));</span><br><span class="line">        <span class="comment">//  等效于printf(&quot;%d \n&quot;,*a[i]),进行列的输出;</span></span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">int</span> matrix[<span class="number">3</span>][<span class="number">3</span>]=&#123;&#123;<span class="number">10</span>,<span class="number">2</span>,<span class="number">3</span>&#125;,&#123;<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>&#125;,&#123;<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>&#125;&#125;;</span><br><span class="line">    <span class="type">int</span> (*a)[<span class="number">3</span>];</span><br><span class="line">    a=matrix;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;<span class="number">3</span>;i++)&#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%d \n&quot;</span>,*(a[<span class="number">0</span>]+i));<span class="comment">//进行行的输出（会警告）</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">int</span> matrix[<span class="number">3</span>][<span class="number">3</span>]=&#123;&#123;<span class="number">10</span>,<span class="number">2</span>,<span class="number">3</span>&#125;,&#123;<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>&#125;,&#123;<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>&#125;&#125;;</span><br><span class="line">    <span class="type">int</span> **p;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;<span class="number">3</span>;i++)&#123;</span><br><span class="line">        p=matrix[<span class="number">0</span>]+i;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%d \n&quot;</span>,*p); <span class="comment">//进行行的输出（会警告）</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<h2 id="C的枚举"><a href="#C的枚举" class="headerlink" title="C的枚举"></a>C的枚举</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">enum　枚举名（可省略）　&#123;枚举元素1,枚举元素2,……&#125; 枚举变量;</span><br></pre></td></tr></table></figure>

<p>枚举变量就是输入常量，输出int;</p>
<p>例子</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">enum</span> <span class="title">DAY</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">      MON=<span class="number">1</span>, TUE, WED, THU, FRI, SAT, SUN</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>等价于</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> MON  1</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> TUE  2</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> WED  3</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> THU  4</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> FRI  5</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> SAT  6</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> SUN  7</span></span><br></pre></td></tr></table></figure>

<p>这样是不行的</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> 1  MON</span></span><br></pre></td></tr></table></figure>



<h2 id="结构体"><a href="#结构体" class="headerlink" title="结构体"></a>结构体</h2><p>一个简易样例</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">test</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">int</span> x,y;</span><br><span class="line">&#125;a;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    a.x=<span class="number">1</span>;</span><br><span class="line">    a.y=<span class="number">9</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">test</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">int</span> x,y;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">test</span> <span class="title">a</span>;</span></span><br><span class="line">    a.x=<span class="number">1</span>;</span><br><span class="line">    a.y=<span class="number">9</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>结构体是一种变量</p>
<p>.  是分量运算符，运算符中优先级最高</p>
<p>进行两次声明，第一次声明结构体里有什么</p>
<p>第二次按照模板构建对象</p>
<p>结构体内可以利用先前已定义的结构体</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> A struct jiegouti <span class="comment">//使A与struct student等价</span></span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">date</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">int</span> year;</span><br><span class="line">    <span class="type">int</span> month;</span><br><span class="line">    <span class="type">int</span> date;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">jiegouti</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">int</span> num;</span><br><span class="line">    <span class="type">char</span> name[<span class="number">20</span>];</span><br><span class="line">    <span class="type">char</span> sex;</span><br><span class="line">    <span class="type">int</span> age;</span><br><span class="line">    <span class="type">float</span> score;</span><br><span class="line">    <span class="type">char</span> addr[<span class="number">30</span>];</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">date</span> <span class="title">birthday</span>;</span></span><br><span class="line">&#125;;</span><br><span class="line">A student1,student2;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    student2.birthday.month=<span class="number">9</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d&quot;</span>,student2.birthday.month);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="指向结构体变量的指针"><a href="#指向结构体变量的指针" class="headerlink" title="指向结构体变量的指针"></a><strong>指向结构体变量的指针</strong></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">student</span> *<span class="title">p</span>;</span>  <span class="comment">//结构一定要一样</span></span><br></pre></td></tr></table></figure>

<p><strong>结构体变量可直接赋值给另一个具有相同结构的结构体变量</strong></p>
<p>一种初始化方法：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> A struct jiegouti</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">jiegouti</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">int</span> num;</span><br><span class="line">    <span class="type">char</span> name[<span class="number">20</span>];</span><br><span class="line">    <span class="type">char</span> sex;</span><br><span class="line">    <span class="type">char</span> addr[<span class="number">30</span>];</span><br><span class="line">&#125;a=&#123;<span class="number">89031</span>,<span class="string">&quot;Li lin&quot;</span>,<span class="string">&#x27;M&#x27;</span>,<span class="string">&quot;123 Beijing Road&quot;</span>&#125;;<span class="comment">//简单方式</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%ld  %s  %c  %s &quot;</span>,a.num,a.name,a.sex,a.addr);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>另一种初始化方法（走错片场，仅cpp可用）</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">node</span>&#123;</span><br><span class="line">    <span class="type">int</span> value;</span><br><span class="line">    node *l,*r;</span><br><span class="line">    <span class="built_in">node</span>(<span class="type">int</span> value =<span class="number">0</span>,node *l = <span class="literal">NULL</span>,node *r = <span class="literal">NULL</span>):<span class="built_in">value</span>(value),<span class="built_in">l</span>(l),<span class="built_in">r</span>(r)&#123;&#125; <span class="comment">//完成初始化</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<p>允许存在<strong>结构体数组</strong>，通过循环逐个输出数据。</p>
<p>指针只要<strong>取结构体数组的地址</strong>就可以代替原有的数组</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(*p).sum 等价于 p-&gt;num</span><br></pre></td></tr></table></figure>



<h3 id="指向结构体数组的指针"><a href="#指向结构体数组的指针" class="headerlink" title="指向结构体数组的指针"></a>指向结构体数组的指针</h3><p>下面这个例子，通过搬动指针进行输出</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">student</span>&#123;</span></span><br><span class="line">    <span class="type">int</span> num;</span><br><span class="line">    <span class="type">char</span> name[<span class="number">20</span>];</span><br><span class="line">    <span class="type">char</span> sex;</span><br><span class="line">    <span class="type">int</span> age;</span><br><span class="line">&#125;stu[<span class="number">3</span>]=&#123;&#123;<span class="number">10101</span>,<span class="string">&quot;Li Lin&quot;</span>,<span class="string">&#x27;M&#x27;</span>,<span class="number">18</span>&#125;,&#123;<span class="number">10102</span>,<span class="string">&quot;Zhang Fun&quot;</span>,<span class="string">&#x27;M&#x27;</span>,<span class="number">19</span>&#125;,&#123;<span class="number">10104</span>,<span class="string">&quot;Wang Min&quot;</span>,<span class="string">&#x27;F&#x27;</span>,<span class="number">20</span>&#125;&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">student</span> *<span class="title">p</span>;</span></span><br><span class="line">    <span class="keyword">for</span>(p=stu;p&lt;stu+<span class="number">3</span>;p++)&#123;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%5d%-20s%2c%4d\n&quot;</span>,p-&gt;num,p-&gt;name,p-&gt;sex,p-&gt;age);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="Tip：结构体中（-）和（-gt-）的区别"><a href="#Tip：结构体中（-）和（-gt-）的区别" class="headerlink" title="Tip：结构体中（.）和（-&gt;）的区别"></a>Tip：结构体中（.）和（-&gt;）的区别</h3><p>相同点：两个都是二元操作符，其右操作符是成员的名称。<br>不同点：点操作符左边的操作数是一个“结果为结构”的表达式；箭头操作符左边的操作数是一个指向结构的指针。</p>
<p><a target="_blank" rel="noopener external nofollow noreferrer" href="https://blog.csdn.net/alidada_blog/article/details/80353343">https://blog.csdn.net/alidada_blog/article/details/80353343</a></p>
<p>另外一点体会：如果一个函数的传入参数是结构体，且需要该结构体作为返回值的时候，必须采用指针传递的方式，其中对<strong>结构体赋值必须使用箭头运算符</strong>。（！）</p>
<h2 id="链表"><a href="#链表" class="headerlink" title="链表"></a>链表</h2><h3 id="概念"><a href="#概念" class="headerlink" title="概念"></a>概念</h3><p>物理存储单元上<strong>非连续</strong>，<strong>非顺序</strong>结构，节点间靠指针联系</p>
<p>每个节点包含<strong>数据和指针</strong></p>
<p><strong>链表的插入和删除都是常数时间</strong></p>
<p><strong>程序局部性原理</strong>：Cpu会把相邻存储单元的数据导入L1，L2，L3缓存中</p>
<p>数组可使用，但链表不行。</p>
<p>适用于</p>
<p>大内存空间分配</p>
<p>元素频繁删除和插入</p>
<p>头节点可以是第一个节点，但建议是哨兵节点（虚拟节点）。</p>
<p>链表的翻转操作</p>
<p>时间复杂度和空间复杂度？？</p>
<p>太长了，不看了</p>
<p><a target="_blank" rel="noopener external nofollow noreferrer" href="http://mp.weixin.qq.com/s?__biz=MzI2NjI5MzU2Nw==&amp;mid=2247495549&amp;idx=2&amp;sn=7b9391f3ea023c904ee2de0d7a171d5d&amp;chksm=ea92f72ddde57e3b297502256d13d6eb0ce7ed14fb7b367ca84e7804c5254c0fe2e3893f7d70&amp;mpshare=1&amp;scene=23&amp;srcid=1026OwQv1RXoM4yQ4tAxpjVr&amp;sharer_sharetime=1603796194401&amp;sharer_shareid=e37afb434548e8cf438b6feb1a808418#rd">http://mp.weixin.qq.com/s?__biz=MzI2NjI5MzU2Nw==&amp;mid=2247495549&amp;idx=2&amp;sn=7b9391f3ea023c904ee2de0d7a171d5d&amp;chksm=ea92f72ddde57e3b297502256d13d6eb0ce7ed14fb7b367ca84e7804c5254c0fe2e3893f7d70&amp;mpshare=1&amp;scene=23&amp;srcid=1026OwQv1RXoM4yQ4tAxpjVr&amp;sharer_sharetime=1603796194401&amp;sharer_shareid=e37afb434548e8cf438b6feb1a808418#rd</a></p>
<h3 id="实现"><a href="#实现" class="headerlink" title="实现"></a>实现</h3><p>首先，C语言中链表只能用指针解决</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">student</span>&#123;</span></span><br><span class="line"> <span class="type">int</span> num;</span><br><span class="line"> <span class="type">float</span> score;</span><br><span class="line"> <span class="class"><span class="keyword">struct</span> *<span class="title">student</span></span></span><br><span class="line"><span class="class">&#125;</span></span><br></pre></td></tr></table></figure>

<p>即数据加指针</p>
<p><strong>建立链表需要&lt;malloc.h&gt;</strong></p>
<p>建立一个链表需要3个指针</p>
<p>一个用于保存链头，一个创建新空间，另一个将新空间写入数据体</p>
<p><strong>新建立的指针都需要需要初始化</strong>，</p>
<h3 id="sizeof函数"><a href="#sizeof函数" class="headerlink" title="sizeof函数"></a>sizeof函数</h3><p>输出类型字符大小,是一个长整形量</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">sizeof</span>(<span class="type">int</span>);</span><br><span class="line"><span class="keyword">sizeof</span>(<span class="keyword">struct</span> student);</span><br></pre></td></tr></table></figure>



<h3 id="malloc函数"><a href="#malloc函数" class="headerlink" title="malloc函数"></a>malloc函数</h3><p>需要malloc.h，输入一次就申请一次。</p>
<p>栈由操作系统自动分配释放 ，用于存放函数的参数值、局部变量等</p>
<p>堆由开发人员分配和释放， 若开发人员不释放，程序结束时由 OS 回收</p>
<p>该函数用于<strong>申请堆空间</strong>，程序结束后不释放</p>
<p>需要用free函数主动释放</p>
<p>如果没有申请到，就会返回NULL</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> *newArr = (<span class="type">int</span> *)<span class="built_in">malloc</span>(SIZE * <span class="keyword">sizeof</span>(<span class="type">int</span>));</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">free(newarr)</span><br></pre></td></tr></table></figure>



<h3 id="typedef函数"><a href="#typedef函数" class="headerlink" title="typedef函数"></a>typedef函数</h3><p>定义类型，用于简化编程</p>
<p><strong>typedef</strong> 是由编译器执行解释的，**#define** 语句是由预编译器进行处理的。</p>
<p>typedef定义语句要用分号结束</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">typedef int[20] TEST</span><br><span class="line">//TEST定义就等价于int[20]</span><br></pre></td></tr></table></figure>

<p>在下面数据体构成链表</p>
<h3 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;malloc.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">stduent</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">long</span> num;</span><br><span class="line">    <span class="type">float</span> score;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">student</span> *<span class="title">next</span>;</span></span><br><span class="line">&#125; STU;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> LEN sizeof(STU)</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> n=<span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">STU *<span class="title function_">creat</span><span class="params">()</span>  <span class="comment">//创建链表</span></span><br><span class="line">&#123;</span><br><span class="line">    STU *head;</span><br><span class="line">    STU *p1,*p2;</span><br><span class="line"></span><br><span class="line">    p1=(STU *)<span class="built_in">malloc</span>(LEN);</span><br><span class="line">    p2=p1;</span><br><span class="line">    <span class="comment">//以下过程为初始化</span></span><br><span class="line">    p1-&gt;num=<span class="number">5</span>;</span><br><span class="line">    p1-&gt;score=<span class="number">20</span>;</span><br><span class="line">    p1-&gt;next=<span class="literal">NULL</span>;</span><br><span class="line">    <span class="comment">//scanf(&quot;%ld,%f&quot;,&amp;p1-&gt;num,&amp;p1-&gt;score);</span></span><br><span class="line">    head=p1;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span>(p1-&gt;num!=<span class="number">0</span>)&#123;</span><br><span class="line">       <span class="comment">/* n=n+1;</span></span><br><span class="line"><span class="comment">        if (n==1)head=p1;</span></span><br><span class="line"><span class="comment">        else p2-&gt;next=p1;</span></span><br><span class="line"><span class="comment">        p2=p1; */</span></span><br><span class="line">        p1=(STU *)<span class="built_in">malloc</span>(LEN);  <span class="comment">//申请新空间</span></span><br><span class="line">        p2-&gt;next=p1;</span><br><span class="line">        p2 =p1;</span><br><span class="line">        <span class="built_in">scanf</span>(<span class="string">&quot;%ld,%f&quot;</span>,&amp;p1-&gt;num,&amp;p1-&gt;score);</span><br><span class="line">        p1-&gt;next=<span class="literal">NULL</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//p2-&gt;next=NULL;</span></span><br><span class="line">    <span class="comment">//free(p1);</span></span><br><span class="line">    <span class="keyword">return</span> head;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    STU * p = creat();</span><br><span class="line">    <span class="keyword">while</span> (p != <span class="literal">NULL</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%ld, %f\n&quot;</span>,p-&gt;num, p-&gt;score);</span><br><span class="line">        p = p-&gt;next;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//printf(&quot;%d&quot;, sizeof(STU));</span></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注意指针类型，定义时记得是struct student</p>
<h4 id="删除所有节点"><a href="#删除所有节点" class="headerlink" title="删除所有节点"></a>删除所有节点</h4><p>直接删除所有节点</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">release</span><span class="params">(STU* p)</span>&#123;</span><br><span class="line">    <span class="keyword">while</span> (p != <span class="literal">NULL</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        STU *o=<span class="literal">NULL</span>;</span><br><span class="line">        o = p-&gt;next;</span><br><span class="line">        <span class="built_in">free</span>(p);</span><br><span class="line">        p=o;</span><br><span class="line">        n++;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;n=%d&quot;</span>, n);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>边输出边删除所有</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">p=head;</span><br><span class="line">    STU *d=NULL;</span><br><span class="line">    for(int i=0;i&lt;4;i++)&#123;</span><br><span class="line">        p = p-&gt;next;</span><br><span class="line">    &#125;</span><br><span class="line">    d=p-&gt;next;</span><br><span class="line">    printf(&quot;%d, %d, %d\n&quot;, d-&gt;num, p-&gt;num);</span><br><span class="line">    free(p);</span><br><span class="line">    p=head;</span><br><span class="line">    for(int i=0;i&lt;3;i++)&#123;</span><br><span class="line">        p = p-&gt;next;</span><br><span class="line">    &#125;</span><br><span class="line">    printf(&quot;%d\n&quot;,p-&gt;num);</span><br><span class="line">    p-&gt;next=d;</span><br></pre></td></tr></table></figure>

<h4 id="删除特定节点"><a href="#删除特定节点" class="headerlink" title="删除特定节点"></a>删除特定节点</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">STU* <span class="title function_">delete</span><span class="params">(STU* link, <span class="type">long</span> value)</span>&#123;</span><br><span class="line">    STU* link1=<span class="literal">NULL</span>;</span><br><span class="line">    STU* linktem=link;</span><br><span class="line">    <span class="keyword">if</span>(linktem-&gt;score!=value)&#123;</span><br><span class="line">        <span class="keyword">do</span>&#123;</span><br><span class="line">            link1=linktem;</span><br><span class="line">            linktem=linktem-&gt;next;</span><br><span class="line">            <span class="keyword">if</span>(linktem-&gt;next==<span class="literal">NULL</span>)</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">while</span>(linktem-&gt;score!=value);</span><br><span class="line">        link1-&gt;next=linktem-&gt;next;</span><br><span class="line"></span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;Testing: %ld, %f\n&quot;</span>,linktem-&gt;num, linktem-&gt;score);</span><br><span class="line">        <span class="built_in">free</span>(linktem);</span><br><span class="line">        <span class="keyword">return</span>(link);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> </span><br><span class="line">    &#123;</span><br><span class="line">        linktem=linktem-&gt;next;</span><br><span class="line">        <span class="built_in">free</span>(link);</span><br><span class="line">        link=linktem;</span><br><span class="line">        <span class="keyword">return</span>(link);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>关键：<strong>要解决死循环，首个项满足的情况</strong></p>
<p><strong>头节点不要轻易删改，用临时变量暂时替代</strong></p>
<p>下面一个清晰思路，可以清晰看清楚是头节点还是尾节点（目前仅仅是一个框架）</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span>(link1 != <span class="literal">NULL</span>)&#123;</span><br><span class="line">        <span class="keyword">if</span>(<span class="number">1</span> &amp;&amp; linktem == link1)&#123;  <span class="comment">//header</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span>(<span class="number">1</span> &amp;&amp; link1-&gt;next == <span class="literal">NULL</span>)&#123; <span class="comment">//trail</span></span><br><span class="line">            </span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;  <span class="comment">//medium</span></span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        linktem = link1;</span><br><span class="line">        link1 = link1 -&gt; next;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h4 id="在特定节点后面增加节点"><a href="#在特定节点后面增加节点" class="headerlink" title="在特定节点后面增加节点"></a>在特定节点后面增加节点</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">add</span><span class="params">(STU* link, <span class="type">long</span> value)</span>&#123;</span><br><span class="line">    STU* link1=<span class="literal">NULL</span>;</span><br><span class="line">    STU* linktem=link;</span><br><span class="line">    <span class="keyword">if</span>(linktem-&gt;score!=value)&#123;</span><br><span class="line">        <span class="keyword">do</span>&#123;</span><br><span class="line">            linktem=linktem-&gt;next;</span><br><span class="line">            <span class="keyword">if</span>(linktem-&gt;next==<span class="literal">NULL</span>)</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">while</span>(linktem-&gt;score!=value);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>(linktem-&gt;next!=<span class="literal">NULL</span>)&#123;</span><br><span class="line">            link1=(STU *)<span class="built_in">malloc</span>(LEN);</span><br><span class="line">            link1-&gt;num=<span class="number">5</span>;</span><br><span class="line">            link1-&gt;score=<span class="number">1</span>;</span><br><span class="line">            link1-&gt;next=linktem-&gt;next;</span><br><span class="line">            linktem-&gt;next=link1;</span><br><span class="line">        &#125;</span><br><span class="line">    </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>没有解决头节点问题</strong>，最好是用上面的那个框架</p>
<h2 id="共用体"><a href="#共用体" class="headerlink" title="共用体"></a>共用体</h2><p>同一个内存段可以存放多种类型，<strong>但一瞬间只有一个成员其作用</strong></p>
<p>内存单位以最大成员为准</p>
<p><strong>不能初始化</strong></p>
<p>共用体变量不能用作函数参数，可以使用指向共用体的指针</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">union 共用体</span><br><span class="line">&#123;成员列表</span><br><span class="line"> &#125;变量列表</span><br></pre></td></tr></table></figure>

<p>下面是一个典型案例</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="class"><span class="keyword">struct</span>&#123;</span></span><br><span class="line">    <span class="type">int</span> num;</span><br><span class="line">    <span class="type">char</span> name[<span class="number">10</span>];</span><br><span class="line">    <span class="type">char</span> sex;</span><br><span class="line">    <span class="type">char</span> job;</span><br><span class="line">        <span class="class"><span class="keyword">union</span> </span></span><br><span class="line"><span class="class">        &#123;</span></span><br><span class="line">            <span class="type">int</span> <span class="class"><span class="keyword">class</span>;</span></span><br><span class="line">            <span class="type">char</span> position[<span class="number">10</span>];</span><br><span class="line">        &#125;category;</span><br><span class="line">&#125;person[<span class="number">2</span>];</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">int</span> n,i;</span><br><span class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="number">2</span>;i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">scanf</span>(<span class="string">&quot;%d %s %c %c&quot;</span>,&amp;person[i].num,person[i].name,&amp;person[i].job);</span><br><span class="line">    <span class="keyword">if</span>(person[i].job==<span class="string">&#x27;s&#x27;</span>)</span><br><span class="line">        <span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;person[i].category.class);</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (person[i].job==<span class="string">&#x27;f&#x27;</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">scanf</span>(<span class="string">&quot;%s&quot;</span>,&amp;person[i].category.position);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> <span class="built_in">printf</span>(<span class="string">&quot;input error!&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="number">2</span>;i++)&#123;</span><br><span class="line">        <span class="keyword">if</span>(person[i].job==<span class="string">&#x27;s&#x27;</span>)&#123;</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;%-6d %-10s %-3c %-3c %-6d \n&quot;</span>,); <span class="comment">//需补齐</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> <span class="built_in">printf</span>(<span class="string">&quot;%-6d %-10s %-3c %-3c %-6d \n&quot;</span>,);  <span class="comment">//需补齐</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>用于解决结构体同一位置不同输出，但目前没有看到更多用法</p>
<p>下面是一个出错案例</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"> </span><br><span class="line"><span class="class"><span class="keyword">union</span> <span class="title">Data</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">   <span class="type">int</span> i;</span><br><span class="line">   <span class="type">float</span> f;</span><br><span class="line">   <span class="type">char</span>  str[<span class="number">20</span>];</span><br><span class="line">&#125;;</span><br><span class="line"> </span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">( )</span></span><br><span class="line">&#123;</span><br><span class="line">   <span class="class"><span class="keyword">union</span> <span class="title">Data</span> <span class="title">data</span>;</span>        </span><br><span class="line"> </span><br><span class="line">   data.i = <span class="number">10</span>;</span><br><span class="line">   data.f = <span class="number">220.5</span>;</span><br><span class="line">   <span class="built_in">strcpy</span>( data.str, <span class="string">&quot;C Programming&quot;</span>);</span><br><span class="line"> </span><br><span class="line">   <span class="built_in">printf</span>( <span class="string">&quot;data.i : %d\n&quot;</span>, data.i);</span><br><span class="line">   <span class="built_in">printf</span>( <span class="string">&quot;data.f : %f\n&quot;</span>, data.f);</span><br><span class="line">   <span class="built_in">printf</span>( <span class="string">&quot;data.str : %s\n&quot;</span>, data.str);</span><br><span class="line"> </span><br><span class="line">   <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在VC上会出错</p>
<p><a target="_blank" rel="noopener external nofollow noreferrer" href="https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-3-c4996?redirectedfrom=MSDN&view=vs-2019">MSDN</a></p>
<p><a target="_blank" rel="noopener external nofollow noreferrer" href="https://www.zhihu.com/question/31378813">知乎</a></p>
<p>建议使用下面替代</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">strcpy_s(str, <span class="keyword">sizeof</span>(str), str1)</span><br></pre></td></tr></table></figure>







<h2 id="C的预处理"><a href="#C的预处理" class="headerlink" title="C的预处理"></a>C的预处理</h2><p>所有的预处理器命令都是以井号（#）开头。它必须是第一个非空字符，为了增强可读性，预处理器指令应从第一列开始。下面列出了所有重要的预处理器指令：</p>
<table>
<thead>
<tr>
<th>指令</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>#define</td>
<td>定义宏</td>
</tr>
<tr>
<td>#include</td>
<td>包含一个源代码文件</td>
</tr>
<tr>
<td>#undef</td>
<td>取消已定义的宏</td>
</tr>
<tr>
<td>#ifdef</td>
<td>如果宏已经定义，则返回真</td>
</tr>
<tr>
<td>#ifndef</td>
<td>如果宏没有定义，则返回真</td>
</tr>
<tr>
<td>#if</td>
<td>如果给定条件为真，则编译下面代码</td>
</tr>
<tr>
<td>#else</td>
<td>#if 的替代方案</td>
</tr>
<tr>
<td>#elif</td>
<td>如果前面的 #if 给定条件不为真，当前条件为真，则编译下面代码</td>
</tr>
<tr>
<td>#endif</td>
<td>结束一个 #if……#else 条件编译块</td>
</tr>
<tr>
<td>#error</td>
<td>当遇到标准错误时，输出错误消息</td>
</tr>
<tr>
<td>#pragma</td>
<td>使用标准化方法，向编译器发布特殊的命令到编译器中</td>
</tr>
</tbody></table>
<h3 id="预定义宏"><a href="#预定义宏" class="headerlink" title="预定义宏"></a>预定义宏</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line">main()</span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;File :%s\n&quot;</span>, __FILE__);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;Date :%s\n&quot;</span>, __DATE__);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;Time :%s\n&quot;</span>, __TIME__);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;Line :%d\n&quot;</span>, __LINE__);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>相当好用的预定义</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> CHECK(x,y)(x<span class="string">&lt;Wx &amp;&amp; x&gt;</span>=0 &amp;&amp; y&gt;=0 &amp;&amp; y&lt;Hy)</span></span><br></pre></td></tr></table></figure>

<p>Wx是全局变量，这也是可行的！（DFS中用于判断是否越界）</p>
<h3 id="宏定义"><a href="#宏定义" class="headerlink" title="宏定义"></a>宏定义</h3><p><strong>宏</strong>（Macro），是一种<a target="_blank" rel="noopener external nofollow noreferrer" href="https://baike.baidu.com/item/%E6%89%B9%E9%87%8F%E5%A4%84%E7%90%86/4973973">批量处理</a>的称谓。<a target="_blank" rel="noopener external nofollow noreferrer" href="https://baike.baidu.com/item/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%A7%91%E5%AD%A6/9132">计算机科学</a>里的宏是一种<a target="_blank" rel="noopener external nofollow noreferrer" href="https://baike.baidu.com/item/%E6%8A%BD%E8%B1%A1">抽象</a>（Abstraction），它根据一系列预定义的规则替换一定的文本模式。<a target="_blank" rel="noopener external nofollow noreferrer" href="https://baike.baidu.com/item/%E8%A7%A3%E9%87%8A%E5%99%A8/10418965">解释器</a>或<a target="_blank" rel="noopener external nofollow noreferrer" href="https://baike.baidu.com/item/%E7%BC%96%E8%AF%91%E5%99%A8/8853067">编译器</a>在遇到宏时会自动进行这一模式替换。对于<a target="_blank" rel="noopener external nofollow noreferrer" href="https://baike.baidu.com/item/%E7%BC%96%E8%AF%91%E8%AF%AD%E8%A8%80">编译语言</a>，宏展开在编译时发生，进行宏展开的工具常被称为宏展开器。宏这一术语也常常被用于许多类似的环境中，它们是源自宏展开的概念，这包括键盘宏和<a target="_blank" rel="noopener external nofollow noreferrer" href="https://baike.baidu.com/item/%E5%AE%8F%E8%AF%AD%E8%A8%80/21500677">宏语言</a>。绝大多数情况下，“宏”这个词的使用暗示着将小命令或动作转化为一系列指令。——百度百科</p>
<p>预处理命令可以改变程序设计环境,提高编程效率,它们并不是 C 语言本身的组成部分</p>
<p>如果程序中有大量相同变量数据要重新赋值，使用宏就不用一个一个地去修改。</p>
<p>替换过程是在编译前，只是单纯的做字符替换，所以不占存储空间</p>
<p>宏替换相当于实现了一个函数调用的功能,而事实上,与函数调用相比,<strong>宏调用更能提高</strong> </p>
<p>原因是：<strong>宏替换只占编译时间，不占运行时间</strong></p>
<p>宏延续运算符（\）</p>
<p>一个宏通常写在一个单行上。但是如果宏太长，一个单行容纳不下，则使用宏延续运算符（\）</p>
<p>字符串常量化运算符（#）</p>
<p>在宏定义中，当需要把一个宏的参数转换为字符串常量时，则使用字符串常量化运算符（#）。在宏中使用的该运算符有一个特定的参数或参数列表。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> dprint(i) printf(#i)</span></span><br><span class="line">不完全等效于 </span><br><span class="line"><span class="meta">#<span class="keyword">define</span> dprint(i) printf(<span class="string">&quot;%d&quot;</span>,i)</span></span><br><span class="line">输出的不在是数字，而是字符串</span><br></pre></td></tr></table></figure>



<p>标记粘贴运算符（##）</p>
<p>宏定义内的标记粘贴运算符（##）<strong>会合并两个参数</strong>。它允许在宏定义中两个独立的标记被<strong>合并</strong>为一个标记</p>
<p>缝合怪！！！！</p>
<p>放一个实例</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> tokenpaster(n) printf (<span class="string">&quot;token&quot;</span> #n <span class="string">&quot; = %d&quot;</span>, token##n)</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> token34 = <span class="number">40</span>;</span><br><span class="line"></span><br><span class="line">	tokenpaster(<span class="number">34</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>宏可以有参数！</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> MAX(a,b) (a&gt;b)?a:b</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> YES printf(<span class="string">&quot;YES&quot;</span>)</span></span><br><span class="line">main()</span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> x, y, max;</span><br><span class="line">	x = <span class="number">5</span>;</span><br><span class="line">	y = <span class="number">10</span>;</span><br><span class="line">	max = MAX(x, y);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;max=%d\n&quot;</span>, max);</span><br><span class="line">	YES;</span><br><span class="line">	<span class="comment">/*宏调用*/</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>字符串( “ “ )中永远不包含宏</p>
<h3 id="文件包含"><a href="#文件包含" class="headerlink" title="文件包含"></a>文件包含</h3><p>使用尖括号表示在包含文件目录中去查找(包含目录是由系统的环境变 量进行设置的,一般为<strong>系统头文件的默认存放目录</strong>,比如 Linux 系统在&#x2F;usr&#x2F;include 目录下),而不在源文件的存放目录中查找</p>
<p>使用双引号则表示首先在<strong>当前的源文件目录</strong>中查找, 若未找到才到包含目录中去查找</p>
<h2 id="C的位运算"><a href="#C的位运算" class="headerlink" title="C的位运算"></a>C的位运算</h2><p>表示数值有3种方法：<strong>原码，反码，补码</strong></p>
<p>**原码中的+0与-0的表示不相同，所以不利于计算机计算</p>
<p>反码不多用</p>
<h3 id="Tips-1"><a href="#Tips-1" class="headerlink" title="Tips"></a>Tips</h3><p>以0开始的是8进制数</p>
<p>以0x开始的是16进制</p>
<p><strong>无符号型只能表示正数</strong></p>
<p>存在a&amp;&#x3D;b的情况</p>
<h3 id="补码"><a href="#补码" class="headerlink" title="补码"></a>补码</h3><p>complement number 补码数</p>
<p>正数：其原码，反码，补码相同</p>
<p>负数：先取反（最高位不变），再加1</p>
<p>一个8位，储存值范围在-128-127</p>
<h4 id="得到十进制"><a href="#得到十进制" class="headerlink" title="得到十进制"></a>得到十进制</h4><p>第一步，各位取反</p>
<p>第二步，取十进制</p>
<p>第三步，加上负号，减1</p>
<p><strong>计算机用补码形式存放数据</strong></p>
<p>补码相加，得到相加数据的补码</p>
<h3 id="位运算符"><a href="#位运算符" class="headerlink" title="位运算符"></a>位运算符</h3><p>&amp; 按位与</p>
<p>|  按位或</p>
<p>^  按位异或  （相同为零，不同为或）</p>
<p>~  取反运算符（是波浪线）(按位取反)</p>
<p>&lt;&lt; 左移运算符</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt; 右移运算符</span><br></pre></td></tr></table></figure>

<p>位移到超过位就会废弃</p>
<h3 id="例子"><a href="#例子" class="headerlink" title="例子"></a>例子</h3><p>例子很少</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">unsigned</span> a,b,c,d;</span><br><span class="line">    a=<span class="number">0331</span>;</span><br><span class="line">    b=a&gt;&gt;<span class="number">4</span>;</span><br><span class="line">    c=~(~<span class="number">0</span>&lt;&lt;<span class="number">4</span>);</span><br><span class="line">    d=b&amp;c;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%o \n %o \n&quot;</span>,a,d);</span><br><span class="line">&#125;  <span class="comment">//取整数a从右端开始的4-7位</span></span><br></pre></td></tr></table></figure>

<p>通过位段将字节拆分存储数字</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">DATA</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">unsigned</span> a:<span class="number">2</span>;</span><br><span class="line">    <span class="type">unsigned</span> b:<span class="number">3</span>;</span><br><span class="line">    <span class="type">unsigned</span> c:<span class="number">4</span>;</span><br><span class="line">    <span class="type">int</span> i;</span><br><span class="line">&#125;data;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    data.a=<span class="number">2</span>;</span><br><span class="line">    data.b=<span class="number">7</span>;</span><br><span class="line">    data.c=<span class="number">9</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d&quot;</span>,data.a);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="C的文件读写"><a href="#C的文件读写" class="headerlink" title="C的文件读写"></a>C的文件读写</h2><p><strong>文件分为ASCII文件（文本文件）与二进制文件</strong></p>
<p>C文件对文件的存取是以字节为单位的</p>
<p>有些C对FILE的定义：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span></span></span><br><span class="line"><span class="class">&#123;</span><span class="type">int</span> -fd;</span><br><span class="line"><span class="type">int</span> -cleft;</span><br><span class="line"><span class="type">int</span> -mode</span><br><span class="line"><span class="type">char</span> *-nextc;</span><br><span class="line"><span class="type">char</span> *-buff;</span><br><span class="line">&#125;FILE;</span><br></pre></td></tr></table></figure>

<h3 id="fopen与fclose"><a href="#fopen与fclose" class="headerlink" title="fopen与fclose"></a>fopen与fclose</h3><p>您可以使用 <strong>fopen( )</strong> 函数来创建一个新的文件或者打开一个已有的文件，这个调用会<strong>初始化类型 FILE</strong> 的一个对象，类型 <strong>FILE</strong> 包含了所有用来控制流的必要的信息。下面是这个函数调用的原型：</p>
<p><strong>FILE一定要大写</strong></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">FILE *fp；</span><br><span class="line">fp=fopen(<span class="string">&quot;/tmp/test.txt&quot;</span>, <span class="string">&quot;w+&quot;</span>);</span><br><span class="line">文件名，使用文件的方式</span><br></pre></td></tr></table></figure>

<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">FILE *fp = <span class="literal">NULL</span>;</span><br></pre></td></tr></table></figure>

<p>fp是指向FILE类型结构体的指针边练，使fp指向某一个文件的结构体变量，<strong>使文件指针变量找到与它相关的文件</strong></p>
<p>输出失败会返回EOF</p>
<p>EOF是stdio.h中定义的符号常量，值为-1</p>
<table>
<thead>
<tr>
<th>r</th>
<th>打开一个已有的文本文件，允许读取文件。</th>
</tr>
</thead>
<tbody><tr>
<td>w</td>
<td>打开一个文本文件，允许写入文件。如果文件不存在，则会创建一个新文件。在这里，您的程序会从文件的开头写入内容。如果文件存在，则该会被截断为零长度，重新写入。</td>
</tr>
<tr>
<td>a</td>
<td>打开一个文本文件，以追加模式写入文件。如果文件不存在，则会创建一个新文件。在这里，<strong>您的程序会在已有的文件内容中追加内容</strong>。</td>
</tr>
<tr>
<td>r+</td>
<td>打开一个文本文件，允许读写文件。</td>
</tr>
<tr>
<td>w+</td>
<td>打开一个文本文件，允许读写文件。如果文件已存在，<strong>则文件会被截断为零长度</strong>，如果文件不存在，则会创建一个新文件。(相当于覆写)</td>
</tr>
<tr>
<td>a+</td>
<td>打开一个文本文件，允许读写文件。如果文件不存在，则会创建一个新文件。读取会从文件的开头开始，写入则只能是追加模式。</td>
</tr>
</tbody></table>
<p>add 追加的意思</p>
<p>一个用于复制文件的C程序</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc,<span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">   FILE *in,*out;</span><br><span class="line">   <span class="type">char</span> ch;</span><br><span class="line">   <span class="keyword">if</span>(argc!=<span class="number">3</span>)</span><br><span class="line">   &#123;</span><br><span class="line">       <span class="built_in">printf</span>(<span class="string">&quot;You forget to enter a file name&quot;</span>);</span><br><span class="line">       <span class="built_in">exit</span>(<span class="number">0</span>);</span><br><span class="line">   &#125;</span><br><span class="line">   <span class="keyword">if</span>((in=fopen(argv[<span class="number">1</span>],<span class="string">&quot;r&quot;</span>))==<span class="literal">NULL</span>)</span><br><span class="line">   &#123;</span><br><span class="line">       <span class="built_in">printf</span>(<span class="string">&quot;Cannot open infile\n&quot;</span>);</span><br><span class="line">       <span class="built_in">exit</span>(<span class="number">0</span>);</span><br><span class="line">   &#125;<span class="comment">//常用判断</span></span><br><span class="line">    </span><br><span class="line">   <span class="keyword">if</span>((out=fopen(argv[<span class="number">2</span>],<span class="string">&quot;w&quot;</span>))==<span class="literal">NULL</span>)</span><br><span class="line">   &#123;</span><br><span class="line">       <span class="built_in">printf</span>(<span class="string">&quot;Cannot open outfile\n&quot;</span>);</span><br><span class="line">       <span class="built_in">exit</span>(<span class="number">0</span>);</span><br><span class="line">   &#125;</span><br><span class="line">   <span class="keyword">while</span>(!feof(in))<span class="comment">//只要feof函数不是零，就一直复制</span></span><br><span class="line">   fputc(fgetc(in),out);</span><br><span class="line">   fclose(in);</span><br><span class="line">   fclose(out);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>exit函数是stdlib里面确定的，用于退出整个程序</p>
<p>主要区别如下</p>
<ol>
<li><p>return返回函数值，是关键字； exit 是一个函数。</p>
</li>
<li><p>return是语言级别的，它表示了调用堆栈的返回；而exit是系统调用级别的，它表示了一个进程的结束。</p>
</li>
<li><p>return是函数的退出(返回)；exit是进程的退出。</p>
</li>
<li><p>return是C语言提供的，exit是操作系统提供的（或者函数库中给出的）。</p>
</li>
</ol>
<p>exit（0）：正常运行程序并退出程序。<br>exit（1）：非正常运行导致退出程序；</p>
<p>return（）：返回函数，若在主函数中，则会退出函数并返回一值。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">fclose(fp);</span><br></pre></td></tr></table></figure>

<p>需要养成关闭文件的习惯。</p>
<h3 id="fgetc与fputc"><a href="#fgetc与fputc" class="headerlink" title="fgetc与fputc"></a>fgetc与fputc</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">fputc(ch,fp)</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ch=fgetc(fp);</span><br></pre></td></tr></table></figure>

<p>ch是字符常量</p>
<p>下面是写入并读取存入字符</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    FILE *fp=<span class="literal">NULL</span>;</span><br><span class="line">    <span class="type">char</span> ch=<span class="string">&#x27;a&#x27;</span>;</span><br><span class="line">    fp=fopen(<span class="string">&quot;Hello.txt&quot;</span>,<span class="string">&quot;w+&quot;</span>);</span><br><span class="line">    <span class="keyword">if</span>(fp==<span class="literal">NULL</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;Cannot open infile\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    fputc(<span class="string">&#x27;C&#x27;</span>,fp);</span><br><span class="line">    rewind(fp);</span><br><span class="line">    ch=fgetc(fp);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%c&quot;</span>,ch);</span><br><span class="line">    fclose(fp);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>如果需要进行大规模读写，不推荐这种方式</p>
<h3 id="feof"><a href="#feof" class="headerlink" title="feof"></a>feof</h3><p>eof是文件结束标志</p>
<p>(End of file)</p>
<p>feof判断文件是否真的结束</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">feof</span><span class="params">(FILE *stream)</span></span><br></pre></td></tr></table></figure>

<p>设置了与流关联的文件结束标识符时，该函数返回一个非零值，否则返回0</p>
<p>文件结束，feof(fp)的值为1，否则为0；</p>
<h3 id="fread与fwrite"><a href="#fread与fwrite" class="headerlink" title="fread与fwrite"></a>fread与fwrite</h3><p>读写数据块</p>
<p>下面这个案例写入一个字符a</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    FILE *fp=<span class="literal">NULL</span>;</span><br><span class="line">    fp=fopen(<span class="string">&quot;Hello.txt&quot;</span>,<span class="string">&quot;w+&quot;</span>);</span><br><span class="line">    <span class="keyword">if</span>(fp==<span class="literal">NULL</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;Cannot open in file\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="type">char</span> f=<span class="string">&#x27;a&#x27;</span>;</span><br><span class="line">    fwrite(&amp;f,<span class="keyword">sizeof</span>(<span class="type">char</span>),<span class="number">1</span>,fp);</span><br><span class="line">    <span class="comment">//需要存储的变量的指针，空间，读写次数，文件指针</span></span><br><span class="line">    fclose(fp);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>结构体，int型只能保存成二进制</p>
<p>貌似只有char类型才能保存成文本文件</p>
<p>读取次数一般写成1，数组或大空间的建议多次循环</p>
<h3 id="fprint与fscanf"><a href="#fprint与fscanf" class="headerlink" title="fprint与fscanf"></a>fprint与fscanf</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">fscanf</span>(fp, <span class="string">&quot;%s&quot;</span>, buff);</span><br></pre></td></tr></table></figure>

<p>输入时要将ASCII转化为二进制</p>
<p>输出时有需要将二进制转化为ASCII</p>
<p>便于理解，但是效率太低</p>
<h3 id="rewind函数"><a href="#rewind函数" class="headerlink" title="rewind函数"></a>rewind函数</h3><p>使位置指针返回文件的开头</p>
<p><strong>无返回值</strong></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">rewind(fp);</span><br></pre></td></tr></table></figure>



<h3 id="fseek函数"><a href="#fseek函数" class="headerlink" title="fseek函数"></a>fseek函数</h3><p><strong>一般用于二进制文件</strong>，文本文件转化会发生位置偏差</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">fseek(fp,<span class="number">100L</span>,<span class="number">0</span>);</span><br><span class="line"><span class="comment">//位置指针移到离文件头100个字节</span></span><br><span class="line">fseek(fp,<span class="number">50L</span>,<span class="number">1</span>);</span><br><span class="line"><span class="comment">//位置指针移到离当前位置的50个字节</span></span><br><span class="line">fseek(fp,<span class="number">-10L</span>,<span class="number">2</span>);</span><br></pre></td></tr></table></figure>

<p>负号前移，正号后移</p>
<p>就算不输入L，<strong>默认是L</strong></p>
<table>
<thead>
<tr>
<th>SEEK_SET</th>
<th>文件的开头（0）</th>
</tr>
</thead>
<tbody><tr>
<td>SEEK_CUR</td>
<td>文件指针的当前位置（1）</td>
</tr>
<tr>
<td>SEEK_END</td>
<td>文件的末尾（2）</td>
</tr>
</tbody></table>
<h3 id="ftell函数"><a href="#ftell函数" class="headerlink" title="ftell函数"></a>ftell函数</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">printf</span>(<span class="string">&quot;%ld&quot;</span>,ftell(fp));</span><br></pre></td></tr></table></figure>

<p>输出的long int型,表示字节数</p>
<h3 id="ferror与clearerr"><a href="#ferror与clearerr" class="headerlink" title="ferror与clearerr"></a>ferror与clearerr</h3><p>文件操作出错，ferror（fp）为1，</p>
<p>使用clearerr，使ferror与ferror值变为0</p>
<h2 id="C的库"><a href="#C的库" class="headerlink" title="C的库"></a>C的库</h2><h3 id="math-h"><a href="#math-h" class="headerlink" title="math.h"></a>math.h</h3><p>you need to compile with the link flag <code>-lm</code>, like this:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">gcc fib.c -lm -o fibo</span><br></pre></td></tr></table></figure>

<p>This will tell gcc to link your code against the math lib. Just be sure to put the flag <strong>after</strong> the objects you want to link. </p>
<h4 id="sqrt"><a href="#sqrt" class="headerlink" title="sqrt()"></a>sqrt()</h4><p>开根号</p>
<h4 id="fabs"><a href="#fabs" class="headerlink" title="fabs()"></a>fabs()</h4><p>取绝对值</p>
<h4 id="pow"><a href="#pow" class="headerlink" title="pow()"></a>pow()</h4><p>平方</p>
<h4 id="exp"><a href="#exp" class="headerlink" title="exp()"></a>exp()</h4><p>e为底</p>
<h4 id="log10"><a href="#log10" class="headerlink" title="log10()"></a>log10()</h4><p>就是lg，ln是log()</p>
<h4 id="sin-与cos"><a href="#sin-与cos" class="headerlink" title="sin()与cos()"></a>sin()与cos()</h4><p>使用弧度制</p>
<p>默认均为double</p>
<p>比如sin30</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sin</span>(<span class="number">30</span>*pi/<span class="number">180</span>);</span><br></pre></td></tr></table></figure>



<h4 id="acos"><a href="#acos" class="headerlink" title="acos()"></a>acos()</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">float PI = acos(-1.0);</span><br></pre></td></tr></table></figure>

<p>C 库函数 <strong>double acos(double x)</strong> 返回以弧度表示的 <strong>x</strong> 的反余弦。</p>
<h3 id="limits-h"><a href="#limits-h" class="headerlink" title="limits.h"></a>limits.h</h3><p>INT_MAX</p>
<p>INT_MIN</p>
<h3 id="stdlib-h"><a href="#stdlib-h" class="headerlink" title="stdlib.h"></a>stdlib.h</h3><h4 id="qsort"><a href="#qsort" class="headerlink" title="qsort()"></a>qsort()</h4><p>以指针为调用条件，感觉不妙</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">qsort</span><span class="params">(<span class="type">void</span> *base, <span class="type">size_t</span> nitems, <span class="type">size_t</span> size, <span class="type">int</span> (*compar)(<span class="type">const</span> <span class="type">void</span> *, <span class="type">const</span> <span class="type">void</span>*))</span></span><br></pre></td></tr></table></figure>

<ul>
<li><strong>base</strong>  – 指向要排序的数组的第一个元素的指针。</li>
<li><strong>nitems</strong>  – 由 base 指向的数组中元素的个数。</li>
<li><strong>size</strong>  – 数组中每个元素的大小，以字节为单位。</li>
<li><strong>compar</strong>  – 用来比较两个元素的函数。</li>
</ul>
<p><strong>两个形参必须是 const void * 型！！</strong></p>
<p>如果 compar 返回值小于 0（&lt; 0），那么 p1 所指向元素会被排在p2所指向元素的前面</p>
<p>如果 compar 返回值等于 0（&#x3D; 0），那么 p1 所指向元素与 p2 所指向元素的顺序不确定</p>
<p>如果 compar 返回值大于 0（&gt; 0），那么 p1 所指向元素会被排在 p2 所指向元素的后面。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> values[] = &#123; <span class="number">88</span>, <span class="number">56</span>, <span class="number">100</span>, <span class="number">2</span>, <span class="number">25</span> &#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">cmpfunc</span> <span class="params">(<span class="type">const</span> <span class="type">void</span> * a, <span class="type">const</span> <span class="type">void</span> * b)</span></span><br><span class="line">&#123;</span><br><span class="line">   <span class="keyword">return</span> ( *(<span class="type">int</span>*)a - *(<span class="type">int</span>*)b );</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">   <span class="type">int</span> n;</span><br><span class="line"></span><br><span class="line">   <span class="built_in">printf</span>(<span class="string">&quot;排序之前的列表：\n&quot;</span>);</span><br><span class="line">   <span class="keyword">for</span>( n = <span class="number">0</span> ; n &lt; <span class="number">5</span>; n++ ) &#123;</span><br><span class="line">      <span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>, values[n]);</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   qsort(values, <span class="number">5</span>, <span class="keyword">sizeof</span>(<span class="type">int</span>), cmpfunc);</span><br><span class="line"></span><br><span class="line">   <span class="built_in">printf</span>(<span class="string">&quot;\n排序之后的列表：\n&quot;</span>);</span><br><span class="line">   <span class="keyword">for</span>( n = <span class="number">0</span> ; n &lt; <span class="number">5</span>; n++ ) &#123;</span><br><span class="line">      <span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>, values[n]);</span><br><span class="line">   &#125;</span><br><span class="line">  </span><br><span class="line">  <span class="keyword">return</span>(<span class="number">0</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>对double类型数组排序（特别要注意）** </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">double</span> in[<span class="number">100</span>]; </span><br><span class="line"> </span><br><span class="line"><span class="type">int</span> <span class="title function_">cmp</span><span class="params">( <span class="type">const</span> <span class="type">void</span> *a , <span class="type">const</span> <span class="type">void</span> *b )</span> </span><br><span class="line">&#123; </span><br><span class="line">  <span class="keyword">return</span> *(<span class="type">double</span> *)a &gt; *(<span class="type">double</span> *)b ? <span class="number">1</span> : <span class="number">-1</span>; </span><br><span class="line">&#125; </span><br><span class="line">qsort(in,<span class="number">100</span>,<span class="keyword">sizeof</span>(in[<span class="number">0</span>]),cmp);</span><br></pre></td></tr></table></figure>

<p>有些奇怪，为什么会这样？</p>
<h4 id="abs"><a href="#abs" class="headerlink" title="abs()"></a>abs()</h4><p>C 库函数 <strong>int abs(int x)</strong> 返回 <strong>x</strong> 的绝对值。</p>
<h4 id="labs（）"><a href="#labs（）" class="headerlink" title="labs（）"></a>labs（）</h4><p>适用于long int,没测试过</p>
<h3 id="string-h"><a href="#string-h" class="headerlink" title="string.h"></a>string.h</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int strcmp(const char *str1, const char *str2)</span><br></pre></td></tr></table></figure>

<p>进入的是变量，返回0和1</p>
<h4 id="memset"><a href="#memset" class="headerlink" title="memset()"></a>memset()</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">char</span> str[<span class="number">100</span>];</span><br><span class="line"><span class="built_in">memset</span>(str,<span class="number">0</span>,<span class="number">100</span>）；</span><br></pre></td></tr></table></figure>

<p><strong>数组初始化！！！</strong></p>
<p>根据空间大小，从前往后初始化</p>
<p><a target="_blank" rel="noopener external nofollow noreferrer" href="https://baike.baidu.com/item/memset/4747579?fr=aladdin#5">https://baike.baidu.com/item/memset/4747579?fr=aladdin#5</a></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://www.mocusez.site">MocusEZ</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://www.mocusez.site/posts/414f.html">https://www.mocusez.site/posts/414f.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc/4.0" rel="external nofollow noreferrer" target="_blank">CC BY-NC 4.0</a> 许可协议。转载请注明来自 <a href="https://www.mocusez.site" target="_blank">Mox的笔记库</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=238395782,2024873914&amp;fm=26&amp;gp=0.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/ed41.html"><img class="prev-cover" src="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3329910212,2075032998&amp;fm=26&amp;gp=0.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">算法图解学习</div></div></a></div><div class="next-post pull-right"><a href="/posts/8eee.html"><img class="next-cover" src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3286944866,209565252&amp;fm=11&amp;gp=0.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Windows的WSL2+Docker初探</div></div></a></div></nav><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="waline-wrap"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/head.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">MocusEZ</div><div class="author-info__description">探索未曾设想的道路</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">61</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">0</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/mocusez" rel="external nofollow noreferrer" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:285918468@qq.com" rel="external nofollow noreferrer" target="_blank" title="Email"><i class="fas fa-envelope"></i></a><a class="social-icon" href="/atom.xml" target="_blank" title="RSS"><i class="fas fa-rss"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">迎接新的明天</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A4%A7%E5%AD%A6%E8%80%83%E5%89%8D%E7%AA%81%E5%87%BB"><span class="toc-number">1.</span> <span class="toc-text">大学考前突击</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#C%E7%9A%84%E7%BC%96%E8%AF%91%E4%B8%8E%E9%93%BE%E6%8E%A5"><span class="toc-number">2.</span> <span class="toc-text">C的编译与链接</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BC%96%E8%AF%91%E5%99%A8"><span class="toc-number">2.1.</span> <span class="toc-text">编译器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%93%BE%E6%8E%A5%E5%99%A8"><span class="toc-number">2.2.</span> <span class="toc-text">链接器</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E8%BE%93%E5%85%A5%E4%B8%8E%E8%BE%93%E5%87%BA"><span class="toc-number">3.</span> <span class="toc-text">基本输入与输出</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%AE%BAscanf%E7%9A%84%E5%AE%89%E5%85%A8%E6%80%A7"><span class="toc-number">3.1.</span> <span class="toc-text">论scanf的安全性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#scanf"><span class="toc-number">3.2.</span> <span class="toc-text">scanf</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8D%A0%E4%BD%8D%E7%AC%A6"><span class="toc-number">3.3.</span> <span class="toc-text">占位符</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#C%E8%AF%AD%E8%A8%80%E5%90%84%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E6%89%80%E5%8D%A0%E5%AD%97%E8%8A%82%E6%95%B0"><span class="toc-number">3.4.</span> <span class="toc-text">C语言各数据类型所占字节数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#C%E7%9A%84%E5%BC%BA%E5%88%B6%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-number">3.5.</span> <span class="toc-text">C的强制类型转换</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B0%8F%E5%86%99%E8%BD%AC%E5%A4%A7%E5%86%99"><span class="toc-number">3.6.</span> <span class="toc-text">小写转大写</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Tip"><span class="toc-number">3.6.1.</span> <span class="toc-text">Tip</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Exit"><span class="toc-number">3.7.</span> <span class="toc-text">Exit</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BD%AC%E4%B9%89%E5%AD%97%E7%AC%A6"><span class="toc-number">3.8.</span> <span class="toc-text">转义字符</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E8%BF%90%E7%AE%97"><span class="toc-number">3.9.</span> <span class="toc-text">基本运算</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8F%96%E6%A8%A1%E8%BF%90%E7%AE%97"><span class="toc-number">3.9.1.</span> <span class="toc-text">取模运算</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B9%98%E6%96%B9%E8%BF%90%E7%AE%97"><span class="toc-number">3.9.2.</span> <span class="toc-text">乘方运算</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Tip%EF%BC%9A%E5%B0%8F%E6%95%B0%E5%AD%98%E5%82%A8"><span class="toc-number">3.10.</span> <span class="toc-text">Tip：小数存储</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#i"><span class="toc-number">3.11.</span> <span class="toc-text">i++</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E9%80%BB%E8%BE%91"><span class="toc-number">3.12.</span> <span class="toc-text">常见逻辑</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B3%A8%E9%87%8A"><span class="toc-number">3.13.</span> <span class="toc-text">注释</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E5%A4%84%E7%90%86"><span class="toc-number">3.14.</span> <span class="toc-text">字符处理</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E5%AD%98%E5%82%A8"><span class="toc-number">3.14.1.</span> <span class="toc-text">字符存储</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%B8%B8%E8%A7%84%E5%A4%84%E7%90%86"><span class="toc-number">3.14.2.</span> <span class="toc-text">常规处理</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E6%95%B0%E7%BB%84"><span class="toc-number">3.14.3.</span> <span class="toc-text">字符数组</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#C%E7%9A%84%E6%95%B0%E7%BB%84"><span class="toc-number">4.</span> <span class="toc-text">C的数组</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E7%BB%84%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="toc-number">4.1.</span> <span class="toc-text">数组初始化</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#C%E7%9A%84%E5%B8%B8%E9%87%8F"><span class="toc-number">5.</span> <span class="toc-text">C的常量</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#C%E7%9A%84%E5%AD%98%E5%82%A8%E7%B1%BB"><span class="toc-number">6.</span> <span class="toc-text">C的存储类</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#register-%E5%AF%84%E5%AD%98%E5%99%A8%E5%8F%98%E9%87%8F"><span class="toc-number">6.1.</span> <span class="toc-text">register 寄存器变量</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#C%E7%9A%84%E8%BF%90%E7%AE%97"><span class="toc-number">7.</span> <span class="toc-text">C的运算</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%99%A4%E4%BB%A50%E7%9A%84%E6%95%85%E4%BA%8B"><span class="toc-number">7.1.</span> <span class="toc-text">除以0的故事</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%97%A0%E9%99%90%E4%B8%8D%E5%BE%AA%E7%8E%AF%E5%B0%8F%E6%95%B0"><span class="toc-number">7.2.</span> <span class="toc-text">无限不循环小数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#C%E5%BE%AA%E7%8E%AF"><span class="toc-number">8.</span> <span class="toc-text">C循环</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#for%E5%BE%AA%E7%8E%AF"><span class="toc-number">8.1.</span> <span class="toc-text">for循环</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#while%E5%BE%AA%E7%8E%AF"><span class="toc-number">8.2.</span> <span class="toc-text">while循环</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#do-while%E5%BE%AA%E7%8E%AF"><span class="toc-number">8.3.</span> <span class="toc-text">do-while循环</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#brake"><span class="toc-number">8.4.</span> <span class="toc-text">brake</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#continue"><span class="toc-number">8.5.</span> <span class="toc-text">continue</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#C%E5%88%A4%E6%96%AD"><span class="toc-number">9.</span> <span class="toc-text">C判断</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#if%E7%9A%84%E8%AF%AD%E6%B3%95"><span class="toc-number">9.1.</span> <span class="toc-text">if的语法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#switch%E7%9A%84%E8%AF%AD%E6%B3%95"><span class="toc-number">9.2.</span> <span class="toc-text">switch的语法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#do-while-%E5%BE%AA%E7%8E%AF"><span class="toc-number">9.3.</span> <span class="toc-text">do while 循环</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#else"><span class="toc-number">9.4.</span> <span class="toc-text">else</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6-%E4%B8%89%E5%85%83%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">9.5.</span> <span class="toc-text">? : 运算符(三元运算符)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">9.6.</span> <span class="toc-text">逻辑运算符</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#C%E7%9A%84%E5%87%BD%E6%95%B0"><span class="toc-number">10.</span> <span class="toc-text">C的函数</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9F%BA%E7%A1%80%E6%A6%82%E5%BF%B5"><span class="toc-number">10.1.</span> <span class="toc-text">基础概念</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#C%E7%9A%84%E9%80%92%E5%BD%92"><span class="toc-number">10.2.</span> <span class="toc-text">C的递归</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#C%E7%9A%84%E6%8C%87%E9%92%88"><span class="toc-number">11.</span> <span class="toc-text">C的指针</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%8C%87%E9%92%88"><span class="toc-number">11.1.</span> <span class="toc-text">二维数组指针</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E6%95%B0%E7%BB%84%E6%8C%87%E9%92%88"><span class="toc-number">11.2.</span> <span class="toc-text">字符数组指针</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88"><span class="toc-number">11.3.</span> <span class="toc-text">函数指针</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%B0%86%E6%95%B0%E7%BB%84%E4%BC%A0%E9%80%92%E7%BB%99%E5%87%BD%E6%95%B0"><span class="toc-number">11.3.1.</span> <span class="toc-text">将数组传递给函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%BF%94%E5%9B%9E%E6%8C%87%E9%92%88%E7%9A%84%E5%87%BD%E6%95%B0"><span class="toc-number">11.3.2.</span> <span class="toc-text">返回指针的函数</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8C%87%E5%90%91%E6%8C%87%E9%92%88%E7%9A%84%E6%8C%87%E9%92%88"><span class="toc-number">11.4.</span> <span class="toc-text">指向指针的指针</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8C%87%E9%92%88%E6%95%B0%E7%BB%84"><span class="toc-number">11.5.</span> <span class="toc-text">指针数组</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B8%B8%E9%87%8F%E6%8C%87%E9%92%88%E4%B8%8E%E6%8C%87%E9%92%88%E5%B8%B8%E9%87%8F"><span class="toc-number">11.6.</span> <span class="toc-text">常量指针与指针常量</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Main%E7%9A%84%E5%BD%A2%E5%8F%82-%E9%87%8D%E8%A6%81%E5%BD%A9%E8%9B%8B"><span class="toc-number">11.7.</span> <span class="toc-text">Main的形参(重要彩蛋)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Tips"><span class="toc-number">11.8.</span> <span class="toc-text">Tips</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A1%88%E4%BE%8B"><span class="toc-number">11.9.</span> <span class="toc-text">案例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#C%E7%9A%84%E6%9E%9A%E4%B8%BE"><span class="toc-number">12.</span> <span class="toc-text">C的枚举</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%93%E6%9E%84%E4%BD%93"><span class="toc-number">13.</span> <span class="toc-text">结构体</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8C%87%E5%90%91%E7%BB%93%E6%9E%84%E4%BD%93%E5%8F%98%E9%87%8F%E7%9A%84%E6%8C%87%E9%92%88"><span class="toc-number">13.1.</span> <span class="toc-text">指向结构体变量的指针</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8C%87%E5%90%91%E7%BB%93%E6%9E%84%E4%BD%93%E6%95%B0%E7%BB%84%E7%9A%84%E6%8C%87%E9%92%88"><span class="toc-number">13.2.</span> <span class="toc-text">指向结构体数组的指针</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Tip%EF%BC%9A%E7%BB%93%E6%9E%84%E4%BD%93%E4%B8%AD%EF%BC%88-%EF%BC%89%E5%92%8C%EF%BC%88-gt-%EF%BC%89%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-number">13.3.</span> <span class="toc-text">Tip：结构体中（.）和（-&gt;）的区别</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%93%BE%E8%A1%A8"><span class="toc-number">14.</span> <span class="toc-text">链表</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A6%82%E5%BF%B5"><span class="toc-number">14.1.</span> <span class="toc-text">概念</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E7%8E%B0"><span class="toc-number">14.2.</span> <span class="toc-text">实现</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#sizeof%E5%87%BD%E6%95%B0"><span class="toc-number">14.3.</span> <span class="toc-text">sizeof函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#malloc%E5%87%BD%E6%95%B0"><span class="toc-number">14.4.</span> <span class="toc-text">malloc函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#typedef%E5%87%BD%E6%95%B0"><span class="toc-number">14.5.</span> <span class="toc-text">typedef函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E4%BE%8B"><span class="toc-number">14.6.</span> <span class="toc-text">实例</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E6%89%80%E6%9C%89%E8%8A%82%E7%82%B9"><span class="toc-number">14.6.1.</span> <span class="toc-text">删除所有节点</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E7%89%B9%E5%AE%9A%E8%8A%82%E7%82%B9"><span class="toc-number">14.6.2.</span> <span class="toc-text">删除特定节点</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%9C%A8%E7%89%B9%E5%AE%9A%E8%8A%82%E7%82%B9%E5%90%8E%E9%9D%A2%E5%A2%9E%E5%8A%A0%E8%8A%82%E7%82%B9"><span class="toc-number">14.6.3.</span> <span class="toc-text">在特定节点后面增加节点</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%B1%E7%94%A8%E4%BD%93"><span class="toc-number">15.</span> <span class="toc-text">共用体</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#C%E7%9A%84%E9%A2%84%E5%A4%84%E7%90%86"><span class="toc-number">16.</span> <span class="toc-text">C的预处理</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%A2%84%E5%AE%9A%E4%B9%89%E5%AE%8F"><span class="toc-number">16.1.</span> <span class="toc-text">预定义宏</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%8F%E5%AE%9A%E4%B9%89"><span class="toc-number">16.2.</span> <span class="toc-text">宏定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%87%E4%BB%B6%E5%8C%85%E5%90%AB"><span class="toc-number">16.3.</span> <span class="toc-text">文件包含</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#C%E7%9A%84%E4%BD%8D%E8%BF%90%E7%AE%97"><span class="toc-number">17.</span> <span class="toc-text">C的位运算</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Tips-1"><span class="toc-number">17.1.</span> <span class="toc-text">Tips</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%A1%A5%E7%A0%81"><span class="toc-number">17.2.</span> <span class="toc-text">补码</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%BE%97%E5%88%B0%E5%8D%81%E8%BF%9B%E5%88%B6"><span class="toc-number">17.2.1.</span> <span class="toc-text">得到十进制</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%8D%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">17.3.</span> <span class="toc-text">位运算符</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BE%8B%E5%AD%90"><span class="toc-number">17.4.</span> <span class="toc-text">例子</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#C%E7%9A%84%E6%96%87%E4%BB%B6%E8%AF%BB%E5%86%99"><span class="toc-number">18.</span> <span class="toc-text">C的文件读写</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#fopen%E4%B8%8Efclose"><span class="toc-number">18.1.</span> <span class="toc-text">fopen与fclose</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#fgetc%E4%B8%8Efputc"><span class="toc-number">18.2.</span> <span class="toc-text">fgetc与fputc</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#feof"><span class="toc-number">18.3.</span> <span class="toc-text">feof</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#fread%E4%B8%8Efwrite"><span class="toc-number">18.4.</span> <span class="toc-text">fread与fwrite</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#fprint%E4%B8%8Efscanf"><span class="toc-number">18.5.</span> <span class="toc-text">fprint与fscanf</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#rewind%E5%87%BD%E6%95%B0"><span class="toc-number">18.6.</span> <span class="toc-text">rewind函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#fseek%E5%87%BD%E6%95%B0"><span class="toc-number">18.7.</span> <span class="toc-text">fseek函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ftell%E5%87%BD%E6%95%B0"><span class="toc-number">18.8.</span> <span class="toc-text">ftell函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ferror%E4%B8%8Eclearerr"><span class="toc-number">18.9.</span> <span class="toc-text">ferror与clearerr</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#C%E7%9A%84%E5%BA%93"><span class="toc-number">19.</span> <span class="toc-text">C的库</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#math-h"><span class="toc-number">19.1.</span> <span class="toc-text">math.h</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#sqrt"><span class="toc-number">19.1.1.</span> <span class="toc-text">sqrt()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#fabs"><span class="toc-number">19.1.2.</span> <span class="toc-text">fabs()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#pow"><span class="toc-number">19.1.3.</span> <span class="toc-text">pow()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#exp"><span class="toc-number">19.1.4.</span> <span class="toc-text">exp()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#log10"><span class="toc-number">19.1.5.</span> <span class="toc-text">log10()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#sin-%E4%B8%8Ecos"><span class="toc-number">19.1.6.</span> <span class="toc-text">sin()与cos()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#acos"><span class="toc-number">19.1.7.</span> <span class="toc-text">acos()</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#limits-h"><span class="toc-number">19.2.</span> <span class="toc-text">limits.h</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#stdlib-h"><span class="toc-number">19.3.</span> <span class="toc-text">stdlib.h</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#qsort"><span class="toc-number">19.3.1.</span> <span class="toc-text">qsort()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#abs"><span class="toc-number">19.3.2.</span> <span class="toc-text">abs()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#labs%EF%BC%88%EF%BC%89"><span class="toc-number">19.3.3.</span> <span class="toc-text">labs（）</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#string-h"><span class="toc-number">19.4.</span> <span class="toc-text">string.h</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#memset"><span class="toc-number">19.4.1.</span> <span class="toc-text">memset()</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/posts/3e9f.html" title="RMM观察与初探"><img src="https://z1.ax1x.com/2023/10/21/piF47TA.md.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="RMM观察与初探"/></a><div class="content"><a class="title" href="/posts/3e9f.html" title="RMM观察与初探">RMM观察与初探</a><time datetime="2023-10-21T04:30:00.000Z" title="发表于 2023-10-21 12:30:00">2023-10-21</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/5e44.html" title="计算机网络课设——UDP/TCP/TLS Socket实验"><img src="https://s1.ax1x.com/2023/09/09/pP6qXOU.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="计算机网络课设——UDP/TCP/TLS Socket实验"/></a><div class="content"><a class="title" href="/posts/5e44.html" title="计算机网络课设——UDP/TCP/TLS Socket实验">计算机网络课设——UDP/TCP/TLS Socket实验</a><time datetime="2023-09-09T07:10:00.000Z" title="发表于 2023-09-09 15:10:00">2023-09-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/cd44.html" title="JQuery的XSS初探"><img src="https://s1.ax1x.com/2023/09/08/pPyvO0O.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JQuery的XSS初探"/></a><div class="content"><a class="title" href="/posts/cd44.html" title="JQuery的XSS初探">JQuery的XSS初探</a><time datetime="2023-09-08T04:30:00.000Z" title="发表于 2023-09-08 12:30:00">2023-09-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/5862.html" title="生产实习记录"><img src="https://s1.ax1x.com/2023/09/02/pPBH058.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="生产实习记录"/></a><div class="content"><a class="title" href="/posts/5862.html" title="生产实习记录">生产实习记录</a><time datetime="2023-09-02T13:51:00.000Z" title="发表于 2023-09-02 21:51:00">2023-09-02</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/9a9b.html" title="Fedora-CoreOS配置与试用（2023年）"><img src="https://s1.ax1x.com/2023/08/28/pPa8tlF.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Fedora-CoreOS配置与试用（2023年）"/></a><div class="content"><a class="title" href="/posts/9a9b.html" title="Fedora-CoreOS配置与试用（2023年）">Fedora-CoreOS配置与试用（2023年）</a><time datetime="2023-08-28T11:35:00.000Z" title="发表于 2023-08-28 19:35:00">2023-08-28</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2019 - 2023 By MocusEZ</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener external nofollow noreferrer" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener external nofollow noreferrer" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><a href="http://beian.miit.gov.cn/" rel="external nofollow noreferrer"  style="color:#f72b07" target="_blank">闽ICP备2021003009号</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>function loadWaline () {
  function insertCSS () {
    const link = document.createElement("link")
    link.rel = "stylesheet"
    link.href = "https://cdn.jsdelivr.net/npm/@waline/client/dist/waline.min.css"
    document.head.appendChild(link)
  }

  function initWaline () {
    const waline = Waline.init(Object.assign({
      el: '#waline-wrap',
      serverURL: 'https://waline.mocusez.site',
      pageview: ,
      dark: 'html[data-theme="dark"]',
      path: window.location.pathname,
      comment: false,
    }, null))
  }

  if (typeof Waline === 'function') initWaline()
  else {
    insertCSS()
    getScript('https://cdn.jsdelivr.net/npm/@waline/client/dist/waline.min.js').then(initWaline)
  }
}

if ('Waline' === 'Waline' || !false) {
  if (false) btf.loadComment(document.getElementById('waline-wrap'),loadWaline)
  else setTimeout(loadWaline, 0)
} else {
  function loadOtherComment () {
    loadWaline()
  }
}</script></div><script>(function(i,s,o,g,r,a,m){i["DaoVoiceObject"]=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;a.charset="utf-8";m.parentNode.insertBefore(a,m)})(window,document,"script",('https:' == document.location.protocol ? 'https:' : 'http:') + "//widget.daovoice.io/widget/1df8ba05.js","daovoice")
</script><script>var isChatBtn = false
daovoice('init', {
  app_id: '1df8ba05',},{
  launcher: { 
     disableLauncherIcon: isChatBtn // 悬浮 ICON 是否显示
  },
});
daovoice('update');

if (isChatBtn) {
  var chatBtnFn = () => {
    var chatBtn = document.getElementById("chat_btn")
    chatBtn.addEventListener("click", function(){
      daovoice('show')
    });
  }
  chatBtnFn()
} else {
  if (false) {
    function chatBtnHide () {
      daovoice('update', {},{
        launcher: { 
        disableLauncherIcon: true // 悬浮 ICON 是否显示
        },
      });
    }
    function chatBtnShow () {
      daovoice('update', {},{
        launcher: { 
        disableLauncherIcon: false // 悬浮 ICON 是否显示
        },
      });
    }
  }
}</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>